Re.sub does not replace string

Is there a mistake in the below code?

import re
text = 'AFL_v_CalcOneIntAreas (%as_Points[0].ub_X%);\n'

print(re.sub('as_Points[0].ub_X', '0x00', text))

      

Expected Result:

AFL_v_CalcOneIntAreas (%0x00%);

      

but the actual output is the same as the input string, please let me know why it behaves this way?

+3


source to share


5 answers


The symbols [

and ]

mean something in regular expressions, you should avoid them:

>>> re.sub('as_Points\[0\]\.ub_X', '0x00', text)
'AFL_v_CalcOneIntAreas (%0x00%);\n'

      

[a-z]

represents, for example, all lower letters. [...]

are used to mean "nothing in them", so [01]

for 0 or 1.
In your case, 'as_Points[0].ub_X'

really 'as_Points0.ub_X'

.

Note that it .

also has special meanings. This means 1 character. You should also avoid this.




If you don't know if your expression contains characters that you should escape, you can use re.escape :

>>> someExpression = "as_Points[0].ub_X"
>>> re.escape(someExpression)
'as\\_Points\\[0\\]\\.ub\\_X'
>>> re.sub(re.escape(someExpression), '0x00', text)
'AFL_v_CalcOneIntAreas (%0x00%);\n'

      

But if you don't want the power of regex, strings have a replace method:

text.replace('as_Points[0].ub_X','0x00')

      

+2


source


You should avoid [

, ]

and .

:

>>> re.sub('as_Points\[0\]\.ub_X', '0x00', text)

      

.

means "any character", [0]

matches only "0".



You can also do this:

esc = re.escape('as_Points[0].ub_X')  # now '[0]' is treated like the string
                                      # literal '[0]' and not the regex '[0]'
re.sub(esc, '0x00', text)

      

Visit re

module
for more useful functions.

+3


source


Search for special characters in your regular expression. You must avoid them.

text = 'AFL_v_CalcOneIntAreas (%as_Points[0].ub_X%);\n'
print(re.sub('as_Points\[0\]\.ub_X', '0x00', text))

      

+2


source


If you are using regular expressions, you need to avoid backslash type []

characters because they have special meanings.

But you don't need to use regular expressions to replace the literal. Just use replace

:

print(text.replace('as_Points[0].ub_X','0x00'))

      

+2


source


You need to exit [

and ]

, and the point is .

:

print(re.sub('as_Points\[0\]\.ub_X', '0x00', text))
# prints: AFL_v_CalcOneIntAreas (%0x00%);

      

+1


source







All Articles