regex - Regular expression in python -
I'm trying to match the following line
line1 = '# text \ N ' But avoid mail / sub lines like this
' # some text {.blah} \ n '< / Pre>Then in other words there are no spaces and numbers (no punctuation marks) and then after the end of the line.
line2 = re.sub (r '# (\ P +) $', r '# 1 1. .text}', line1)Contains the content of line 1 in line 2 unchanged. (I have read somewhere that \ p is everything except punctuation)
line2 = re.sub (r '# (\ w * \ d * \ s *) + $', R '# \ 1 {.text}', line1)In the above form
'# {.text}'
Your regex is a little weird; Expanded, looks like
r "# ([a-zA-Z0- 9_] * [0-9] * [\ t \ n \ r \ f \ v] * )Things to note:
This is not anchor at the beginning of the string, which means this match
<\ d *is unnecessary, because it is already captured by\ w *It has been done, for example, it seems that you should be less worried about punctuation; The only thing that you can not have is to go Try the functools imported partial def add_text (txt) from
try
< P> which gives: Return re.sub (r ^ ^ ([^ {] *) $ ", r" # 1 1. {Text} ", txt, flags = re.M) text =" # some text \ n # more text {.blah} \ n Print ('ABC') # But do not me! \ N # and print ("=== ===") Print (text) print ("\" N === === ") Print (add_text)=== === # some text # more text {.blah} print ('ABC') # but do not me! #and once again === === Some text {.text} # and text {.blah} print ('ABC') # But do not me! #and once again {.text}
Comments
Post a Comment