re.comp开发者_如何学JAVAile("a[b|c]d").findall("akjh adc abc abbd acd")
why this returns only
['acd']
not
['abd', 'acd']
?
EDIT:
I gave wrong example... thats why. Post can be deleted..
The regular expression will match a string starting with a, followed by b or c and then d. That is:
'abd'
or
'acd'
Your regex matches any string starting with a
, ending with d
and with either a b
, a |
or a c
.
If you are looking for have either a b
or a c
between a
and d
, you use the square brackets without the pipe (a[bc]d
) or you use parenthesis with the pipe (a(b|c)d
). The square brackets means that any character between them is accepted one time (including the |
); the parenthesis means that any of the regexes separated by |
are accepted - and in this case you would have two regexes, one matching b
only and one matching c
only.
I find the first option the best one. However, these regexes would not match abc
in any way. There are a lot of regexes which can match both strings, such as a[bc][cd]
or (which makes more sense to me) a(bc|cd)
.
HTH.
you don't need the pipe in the character class
re.compile("a[bc]d").findall("akjh adc abc abbd acd")
the pattern says, search for a
, then b
or c
(ie, anything in the character class) and then d
. So the regex only returns 1 finding.
精彩评论