Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call functions from re.sub

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?

like image 890
user1586464 Avatar asked Aug 14 '12 02:08

user1586464


People also ask

How do you use re sub function?

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.

Does re sub replace all occurrences?

By default, the count is set to zero, which means the re. sub() method will replace all pattern occurrences in the target string.

Which of following is correct syntax for re sub () in Python?

The syntax for re. sub() is re. sub(pattern,repl,string). That will replace the matches in string with repl.

How do you replace re subs?

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.


2 Answers

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).

like image 110
BrenBarn Avatar answered Oct 01 '22 01:10

BrenBarn


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)
like image 23
xdazz Avatar answered Oct 01 '22 02:10

xdazz