python - Match many results in the same String -
i'm trying data log file lines in different format, guaranteed important information put inside [], example:
[user] has [do something] on [system] @ [time] or
[system] encounters [exception] @ [time] if possible, want write single regular expression information inside each log line, i.e. regex has match many resutls in same line. example:
[admin] has [logged out] on [admin page] @ [monday 20 may, 11:00]returnadmin, logged out, admin page, monday 20 may, 11:00[order page] encounters [nullpointerexception] @ [monday 20 may,return
11:00]orderpage, nullpointerexception, monday 20 may, 11:00
i'm working on python answers in other languages or in pure regular expression fine. thanks
>>> import re >>> text = "[admin] has [logged out] on [admin page] @ [monday 20 may, 11:00]" >>> re.findall(r'\[([^\]]*)\]', text) ['admin', 'logged out', 'admin page', 'monday 20 may, 11:00'] verbose:
>>> text = "[order page] encounters [nullpointerexception] @ [monday 20 may, 11:00]" >>> re.findall(r'''\[ # literal [ character (needs backslash escape) ( # save following group [^\]] # match character except literal ] * # match many possible of these ) # end group \] # literal ] character ''', text, flags=re.verbose) ['order page', 'nullpointerexception', 'monday 20 may, 11:00']
Comments
Post a Comment