\s means the white spaces. It is the same as [ \t\n\r\f\v].
re.sub(pattern, replace_string, original_string)
>>> 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 result is 'static PyObject*\npy_myfunc(void)\n{'
using raw stirng to simplify the problem.
'\\\\section' = r'\\section'
'\\section' =r'\section'
The matching for the phone numbers:
>>> phonePattern = re.compile(r'''
# don't match beginning of string, number can start anywhere
(\d{3}) # area code is 3 digits (e.g. '800')
\D* # optional separator is any number of non-digits
(\d{3}) # trunk is 3 digits (e.g. '555')
\D* # optional separator
(\d{4}) # rest of number is 4 digits (e.g. '1212')
\D* # optional separator
(\d*) # extension is optional and can be any number of digits
$ # end of string
''', re.VERBOSE)
>>> phonePattern.search('work 1-(800) 555.1212 #1234').groups()
('800', '555', '1212', '1234')
>>> phonePattern.search('800-555-1212')
('800', '555', '1212', '')
>>> po = re.compile(r'\w*?e') #nongreedy matching
>>> mo = re.search(po, r'the the')
>>> re.findall(po, r'the the')
result: ['the', 'the']
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.