Monday 15 September 2014

Python regex chokes on \n -


I want to use a regex in Python which reads the text, in all instances & lt; Emotion & gt; Markup exists within the same sentence & lt; Location & gt; Markup, then allows the print of an exclusive line of an output file these sentences is:

  import re-Out = readfile Open ( 'Autktekst', 'w') = "& lt; location & gt; Oklahoma & lt; / location & gt; where does the air & lt; sense & gt; wide & lt; / sense & gt; the & lt; location & gt; simple & lt; / location & gt; and waving wheat. can make it smell & lt; sense & gt; sweet & lt; / to match the spirit & gt;. "Refndl ( '?: (? & Lt; = \.) \ S + | ^) ((? = (?: (?!. (?: \ S | $)).) *? \ Bemotion & gt; (? | \ | = \ S $.)) | * \ Blocation> (= \ s | \ (= (: (\ (\ s $)) ??!?. )?. | $$). *? \. (? = \ S | $)), readfile, flags = re.I): line = ''. (Str (x) for x in the match) out. + '\ N') is out.close ()   

The problem is that if I'm reading in a file that contains line breaks, then regex failed:

  import Reopen = open ( "Autktekst ',' w ') readfile =" & lt; location & gt; Oklahoma & lt; / location & gt; where the air is \ n Arrival & lt; sense & Gt; wide & lt; / emotion & gt; bottom & lt; location & gt; plain & lieutenant; ;;;;;;;;;;;;;;;;;;;;;;;;;;;; l and waving wheat. this odor & lt; sense & gt; sweet & lt; / sense & gt; to match Refndl ( '?: (? & Lt; = \.) \ S + | ^) ((? = (?: (?!! (?: \ S | $)). * *? \ Bemotion & gt; (? | = | = \ $ S.)) | * \ Blocation & gt; (= \ s | \ (= (: (\ ( \ S $)) ??!!?.). | $$). *? \. (? = \ S | $)), readfile, flags = re.I): line = ''. (Match In str (x) out of x. + '\ N') out.close ()   

Is there any way to modify this regular expression, when it hits \ N? I would be most grateful for any advice that can lend to this question.

Re-add (they are the same thing) for the flag in your regex Due to . will also match the new lines. Therefore the new value again for the flag argument Will be. Again s .

No comments:

Post a Comment