Bharadwaj
Bharadwaj

Reputation: 745

re.sub not replacing the 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))

The expected output is

AFL_v_CalcOneIntAreas (%0x00%);

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

Upvotes: 3

Views: 4782

Answers (5)

fredtantini
fredtantini

Reputation: 16556

The symbols [ and ] means something in regular expressions, you have to escape them:

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

[a-z] represents all the lower letters for instance. [...] are used to denote «anything in them» so [01] is for 0 or 1.
In your case 'as_Points[0].ub_X' is in fact 'as_Points0.ub_X'.

Note that the . has special meanings too. It means 1 character. You should also escape it too.


If you don't know if your expression contains characters 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 need regular expression power, strings have the replace method:

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

Upvotes: 2

Maroun
Maroun

Reputation: 95968

You should escape [, ] and .:

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

. means "any character", [0] matches only "0".

You can do this as well:

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 the re module for more useful functions.

Upvotes: 3

khelwood
khelwood

Reputation: 59113

If you're using regular expressions you need to escape characters like [] with backslashes, because they have special meanings.

But you don't need to use regular expressions to replace a literal string. Just use replace:

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

Upvotes: 2

dotcomly
dotcomly

Reputation: 2214

Your searching for special characters in your regex. You have to escape them.

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

Upvotes: 2

Marcin
Marcin

Reputation: 238249

You need to escape [ and ] and the dot .:

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

Upvotes: 1

Related Questions