This is a simple example:
import re
math='<m>3+5</m>'
print re.sub(r'<(.)>(\d+?)\+(\d+?)</\1>', int(r'\2') + int(r'\3'), math)
It gives me this error:
ValueError: invalid literal for int() with base 10: '\\2'
It sends \\2
instead of 3
and 5
.
Why? How do I solve it?
re. sub() function is used to replace occurrences of a particular sub-string with another sub-string. This function takes as input the following: The sub-string to replace. The sub-string to replace with.
By default, the count is set to zero, which means the re. sub() method will replace all pattern occurrences in the target string.
The syntax for re. sub() is re. sub(pattern,repl,string). That will replace the matches in string with repl.
If you want to replace a string that matches a regular expression (regex) instead of perfect match, use the sub() of the re module. In re. sub() , specify a regex pattern in the first argument, a new string in the second, and a string to be processed in the third.
If you want to use a function with re.sub
you need to pass a function, not an expression. As documented here, your function should take the match object as an argument and returns the replacement string. You can access the groups with the usual .group(n)
methods and so on. An example:
re.sub("(a+)(b+)", lambda match: "{0} as and {1} bs ".format(
len(match.group(1)), len(match.group(2))
), "aaabbaabbbaaaabb")
# Output is '3 as and 2 bs 2 as and 3 bs 4 as and 2 bs '
Note that the function should return strings (since they will be put back into the original string).
You need to use lambda function.
print re.sub(r'<(.)>(\d+?)\+(\d+?)</\1>', lambda m: str(int(m.group(2)) + int(m.group(3))), math)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With