python 2.7 - How to select a particular pattern of words -


i trying identify words, notes financial statements. in cases, sentence starts 'notes financial statements', in other cases, starts ' notes financial statements' (that is, there spaces before words). i'd select 'notes financial statements' in these 2 cases, is, sentence contains words starts or without spaces. easy job , know need use regular expression. problem there other cases characters come before words. example, 'accompanying notes financial statements'. so, pattern words, 1 space, notes financial statements. don't want select one.

given text follows:

"""take @ accompanying notes financial statements"""  n1=re.sub(r'\w*notes financial statements','### notes ###',text2) 

the above command selects 'notes financial statements', don't want select because preceded words. commands provides output follows:

"""take @ accompanying ### notes ###"""  

i think because \w* captures 1 space between accompanying , notes. how can make command not select in case 'notes financial statements' thanks.

you need add ^ character, matches start of line, , use \s instead of \w match whitespace:

"""take @ accompanying notes financial statements"""  n1=re.sub(r'^\s*notes financial statements','### notes 

this match if optional spaces , specified phrase first thing(s) on line.

demo

note may want consider adding case-insensitive flag (i), suspect see capital n.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -