nik7
nik7

Reputation: 3058

is this possible in re in python?

In regular expression in python

[abc] matches either a or b or c

How to do [abc] that matches either ab or bc ? Is this possible ?

Upvotes: 0

Views: 99

Answers (2)

stema
stema

Reputation: 93026

[abc] is called a character class. It matches one out of the character set given between the square brackets. This is fine for single characters, but will not work for character sequences.

For a character sequence you need an alternation, this looks like a|b|c. This would be equivalent to [abc], but now you can put in sequences.

ab|bc

would match either "ab" or "bc"

If the alternation is part of a larger expression you need to group it together

(?:ab|bc)

(?:...) is a non capturing group, it will not capture the matched sub-pattern.

Upvotes: 1

Use the | regular expression operator to specify multiple possibilities:

>>> import re
>>> pattern = re.compile("ab|bc")
>>>
>>> print pattern.match("abbbb")
<_sre.SRE_Match object at 0x105117918>
>>> print pattern.match("fooo")
None
>>> print pattern.match("bcdd")
<_sre.SRE_Match object at 0x105117918>
>>> print pattern.match("abc")
<_sre.SRE_Match object at 0x105117918>

Wrap them in brackets if it's part of a larger expression:

>>> pattern = re.compile("(red|green) light")
>>> print pattern.match("red light")
<_sre.SRE_Match object at 0x105117918>
>>> print pattern.match("red lights everywhere")
<_sre.SRE_Match object at 0x1051116c0>
>>> print pattern.match("green lights forever")
<_sre.SRE_Match object at 0x105117918>
>>> print pattern.match("blue lights begone")
None

See regular expression syntax in the Python documentation for all of the options.

Upvotes: 7

Related Questions