Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How vertical bars literal determine the formal regular expression in python?

Tags:

python

regex

According to the python doc, vertical bars literal are used as an 'or' operator. It matches A|B,where A and B can be arbitrary REs.

For example, if the regular expression is as following: ABC|DEF,it matches strings like these:

"ABC", "DEF"

But what if I want to match strings as following:

"ABCF", "ADEF"

Perhaps what I want is something like A(BC)|(DE)F which means:

  • match "A" first,
  • then string "BC" or "DE",
  • then char "F".

I know the above expression is not right since brackets have other meanings in regular expression, just to express my idea.

Thanks!

like image 561
xiao 啸 Avatar asked May 13 '11 07:05

xiao 啸


1 Answers

These will work:

A(BC|DE)F
A(?:BC|DE)F

The difference is the number of groups generated: 1 with the first, 0 with the second.

Yours will match either ABC or DEF, with 2 groups, one containing nothing and the other containing the matched fragment (BC or DE).

like image 187
Ignacio Vazquez-Abrams Avatar answered Nov 14 '22 23:11

Ignacio Vazquez-Abrams