[Tutor] regular expression woes ..
Israel C. Evans
Sun, 05 Aug 2001 21:13:31 -0700
Does anybody know why when I try...
>>> pat = re.compile('_.*' | '\..*')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in ?
pat = re.compile('_.*' | '\..*')
TypeError: unsupported operand type(s) for |
I get the error that I do?
The documentation says:
A|B, where A and B can be arbitrary REs, creates a regular expression
that will match either A or B. An arbitrary number of REs can be separated
by the "|" in this way. This can be used inside groups (see below) as
well. REs separated by "|" are tried from left to right, and the first
one that allows the complete pattern to match is considered the accepted
branch. This means that if A matches, B will never be tested, even if
it would produce a longer overall match. In other words, the "|" operator
is never greedy. To match a literal "|", use \|, or enclose it inside
a character class, as in [|].
It would seem to me that I'm using '|' correctly but obviously I'm not.
I've also tried
>>> pat1 = re.compile('_.*')
>>> pat2 = re.compile('\..*')
>>> allpat = re.compile(pat1 | pat2)
....same error as before...
>>> pat = re.compile(('_.*') | ('\..*'))
FREE voicemail, email, and fax...all in one place.
Sign Up Now! http://www.onebox.com