python - How to format a regex -


i trying make warning waver can known warnings in log file.

the warnings in waving file copied directly log file during review of warnings.

the mission here make simple possible. found directly copying bit problematic due fact warnings contain absolute paths.

so added "tag" inserted warning system should for. whole string this.

warning:hdlparsers:817 - ":re[.*]:/modules/top/hdl_src/top.vhd" line :re[.*]: choice . not locally static expression. 

the tag :re[insert regex here]:. in above warning string there 2 of these tags trying find using python3 regex tool. , pattern following:

(:re\[.*\]\:) 

see regex101 reference

my problem above that, when there 2 tags in string finds 1 result extended first last tag. how setup regex find each tag ?

regards

you can use re.findall following regex assumes regular expression inside square brackets spans :re[ ] followed ]:

:re\[.*?]: 

see regex demo. .*? matches 0 or more characters other newline few possible. see rexegg.com description of lazy quantifier solution:

the lazy .*? guarantees quantified dot matches many characters needed rest of pattern succeed.

see ideone demo

import re p = re.compile(r':re\[.*?]:') test_str = "# more commments\nwarning:hdlparsers:817 - \":re[.*]:/modules/top/hdl_src/cpu_0342.vhd\" line :re[.*]: choice . not locally static expression." print(p.findall(test_str)) 

if need contents between [ , ], use capturing group re.findall extract contents:

p = re.compile(r':re\[(.*?)]:') 

see another demo

to obtain indices, use re.finditer (see this demo):

re.finditer(pattern, string, flags=0)
return iterator yielding match objects on non-overlapping matches re pattern in string. string scanned left-to-right, , matches returned in order found. empty matches included in result unless touch beginning of match.

p = re.compile(r':re\[(.*?)]:') print([x.start(1) x in p.finditer(test_str)]) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -