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:

  1. This is not anchor at the beginning of the string, which means this match

    < \ d * is unnecessary, because it is already captured by \ w *

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

  : 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)  
< P> which gives

  === === # 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

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -