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?
source to share
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')
source to share
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.
source to share