regex - Why do I need to add DOTALL to python regular expression to match new line in raw string -


why 1 need add dotall flag python regular expression match characters including new line character in raw string. ask because raw string supposed ignore escape of special characters such new line character. docs:

the solution use python’s raw string notation regular expression patterns; backslashes not handled in special way in string literal prefixed 'r'. r"\n" two-character string containing '\' , 'n', while "\n" one-character string containing newline.

this situation:

string = '\nsubject sentence is:  appropriate support families of children diagnosed hearing impairment\ncausal verb :  may have\npredicate sentence is:  direct impact on success of hearing detection , intervention programs in reducing negative effects of permanent hearing loss'  re.search(r"subject sentence is:(.*)causal verb :(.*)predicate sentence is:(.*)", string ,re.dotall) 

results in match , , when remove dotall flag, no match.

your source string not raw, pattern string.

maybe try

string = r'\n...\n' re.search("subject sentence is:(.*)causal verb :(.*)predicate sentence is:(.*)", string) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -