Python regex substitution problem

Status
Not open for further replies.

Fuzzmz

Ars Tribunus Militum
1,636
This has me scratching my head for the past hour or so and I've decided to ask for some input and maybe find out what I'm missing.

The point of the script is to convert some IRC logs from one format (bip) to another (Energymech), and to do that I need to make a small regex substitution: I need to change everything between ! and : (them included) to >.

Now, I know I can do this using
Code:
re.sub(r'\!.*?\:', '>', 'test !sx: asd)
(the string is just an example), and if I do that in the python interpreter it gives the correct output, but when I do it in my code it just ignores it for whatever reason.

The code I'm using and a sample log is here.

I'm just stuck right now and to say that it's frustrating would be an understatement.
 

Alamout

Ars Legatus Legionis
27,285
You're not saving the output of the regex. It returns a result, it doesn't modify the string in place (because strings are immutable). The interpreter prints out whatever the result of an expression is, which is a bit confusing.

Quick add-on: if you're going to run this on a lot of text, it would be good to use re.compile to pre-compile the regex before going through the loop.
 

Fuzzmz

Ars Tribunus Militum
1,636
Alamout":1n1a0ugc said:
You're not saving the output of the regex. It returns a result, it doesn't modify the string in place (because strings are immutable). The interpreter prints out whatever the result of an expression is, which is a bit confusing.

Quick add-on: if you're going to run this on a lot of text, it would be good to use re.compile to pre-compile the regex before going through the loop.

Great, that fixed it! Thanks a lot for the help and tip!

Edit: Because it seems that this threads comes out as the first result when searching for bip to energymech I'll just link to the code source here. You can find the converter here.
 
Status
Not open for further replies.