>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
... r'static PyObject*\npy_\1(void)\n{',
... 'def myfunc():')
The answer is : 'static PyObject*\npy_myfunc(void)\n{'
re.sub(pattern, repl, string):
The substitution has two steps:
a. Using pattern to match the string; find the left-most occurance of the pattern. If no substring matches the pattern, the sub func returns the orginal string without modification.
b. Replace the matched substring with the repl. The \number is replaced accordingly.
The use of re.VERBOSE to make the regular exp look nicer.
Whitespace within the pattern is ignored, except when in a character class or preceded by an unescaped backslash, and, when a line contains a '#' neither in a character class or preceded by an unescaped backslash, all characters from the leftmost such '#' through the end of the line are ignored.
That means that the two following regular expression objects that match a decimal number are functionally equal:
a = re.compile(r"""\d + # the integral part
\. # the decimal point
\d * # some fractional digits""", re.X)
b = re.compile(r"\d+\.\d*")
More about regular expression: http://docs.activestate.com/komodo/4.4/regex-intro.html
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.