could you please advice how to verify in python if provided string correspond to provided pattern and return result.
For example the provided pattern is following:
< [prefix]-[id开发者_StackOverflow社区]> separated by ','>|< log >"
where prefix is any number of alphabetic characters, id is only numbers but not exceeding 5 digits, log is any number of any characters
examples:
proj-123|log message
proj-234, proj-345|log message
I suppose the easiest way is to apply regexp which I didn't use on python.
Thanks.
(?:[a-z]+-\d{1,5})(?:, [a-z]+-\d{1,5})*\|.*
it's not clear what you want to capture, that's why I use non-capturing groups. If you need only boolean:
>>> regex = '[a-z]+-\d{1,5}(?:, [a-z]+-\d{1,5})*\|.*'
>>> re.match(regex, 'proj-234, proj-345|log message') is not None
True
Of course, the same result can be achieved with the sequence of simple string methods:
prefs, _, log = subj.partition('|')
for group in prefs.split(', '):
pref, _, id5 = group.partition('-')
if id5.isdigit() and len(id5) <= 5 and pref.isalpha():
print(pref, id5)
Python has great regexp library in stdlib. Here is documentation. Simply use re.match and that should be all you need.
Extending SilentGhosts' excellent regexp...
The following will match more than two comma separated tags and it captures the tags in one group and the log message in another group:
import re
line = 'proj-234,proj-345,proj-543|log message'
match = re.match('((?:[a-zA-Z]+-\d{1,5})(?:,[a-zA-Z]+-\d{1,5})+)\|(.*)', line)
tags = match.group(1).split(',')
log_msg = match.group(2)
I wasn't able to figure out if it was possible to capture the tags following the first tag without capturing the comma, so I decided to capture them in one group and split them after the fact.
精彩评论