Simple regular expression
I'm new to regular expressions and tried to use the following concept:
(?(id/name)yes-pattern|no-pattern)
in this way:
import re
print re.match("(?(\w),+)", "a,,,,,").groups()
Got the following error :
error: 'bad character in group name'.
Unfortunately I couldn't figure out what's wrong about this expression?
开发者_开发问答Thanks in advance!
You didn't write a valid id
or name
.
I found the definition:
(?(id/name)yes-pattern|no-pattern)
Will try to match with
yes-pattern
if the group with givenid
orname
exists, and withno-pattern
if it doesn’t.no-pattern
is optional and can be omitted. For example,(<)?(\w+@\w+(?:\.\w+)+)(?(1)>)
is a poor email matching pattern, which will match with '<user@host.com>
' as well as 'user@host.com
', but not with '<user@host.com
'.New in version 2.4.
In your case, you haven't provided the id/name
of a group to search for. Are you sure that this is the feature you want?
精彩评论