Here is my code:
import re
p = re.compile('(?P<name>[^|]*)|(?P<desc>[^|]*)')
m = p.match('jack|a good boy')
name = m.group('name')
desc = m.group('desc')
print name # jack
print desc # None
How can I get the desc after the ‘|’?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To match a
|you must escape it. Otherwise your pattern would match either first or last group. But, not the both.Note I’ve changed your piece of code a bit. Just escaped
|to\|.