[Tutor] regular expression question

Liam Clarke cyresse at gmail.com
Wed Mar 9 21:28:59 CET 2005


Actually, you should get that anyway...

"""
|
    Alternation, or the ``or'' operator. If A and B are regular
expressions, A|B will match any string that matches either "A" or "B".
| has very low precedence in order to make it work reasonably when
you're alternating multi-character strings. Crow|Servo will match
either "Crow" or "Servo", not "Cro", a "w" or an "S", and "ervo".
"""

So, for each letter in that string, it's checking to see if any letter
matches 'A' or 'B' ...
the engine steps through one character at a time.
sorta like - 

for letter in s:
     if letter == 'A':
        #Do some string stuff
     elif letter == 'B':
        #do some string stuff


i.e. 

k = ['A','B', 'C', 'B']

for i in range(len(k)):
    if k[i] == 'A' or k[i]=='B':
       k[i]==13

print k

[13, 13, 'C', 13]

You can limit substitutions using an optional argument, but yeah, it
seems you're expecting it to examine the string as a whole.


Check out the example here - 
http://www.amk.ca/python/howto/regex/regex.html#SECTION000320000000000000000

Also

http://www.regular-expressions.info/alternation.html

Regards, 

Liam Clarke


On Thu, 10 Mar 2005 09:09:13 +1300, Liam Clarke <cyresse at gmail.com> wrote:
> Hi Mike,
> 
> Do you get the same results for a search pattern of 'A|B'?
> 
> 
> On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall
> <michael.hall at critterpixstudios.com> wrote:
> > I'm having some strange results using the "or" operator.  In every test
> > I do I'm matching both sides of the "|" metacharacter, not one or the
> > other as all documentation says it should be (the parser supposedly
> > scans left to right, using the first match it finds and ignoring the
> > rest). It should only go beyond the "|" if there was no match found
> > before it, no?
> >
> > Correct me if I'm wrong, but your regex is saying "match dog, unless
> > it's followed by cat. if it is followed by cat there is no match on
> > this side of the "|" at which point we advance past it and look at the
> > alternative expression which says to match in front of cat."
> >
> > However, if I run a .sub using your regex on a string contain both dog
> > and cat, both will be replaced.
> >
> > A simple example will show what I mean:
> >
> >  >>> import re
> >  >>> x = re.compile(r"(A) | (B)")
> >  >>> s = "X R A Y B E"
> >  >>> r = x.sub("13", s)
> >  >>> print r
> > X R 13Y13 E
> >
> > ...so unless I'm understanding it wrong, "B" is supposed to be ignored
> > if "A" is matched, yet I get both matched.  I get the same result if I
> > put "A" and "B" within the same group.
> >
> >
> > On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote:
> >
> > >
> > >
> > >>
> > >> Regular expressions are a little evil at times; here's what I think
> > >> you're
> > >> thinking of:
> > >>
> > >> ###
> > >>>>> import re
> > >>>>> pattern = re.compile(r"""dog(?!cat)
> > >> ...                    | (?<=dogcat)""", re.VERBOSE)
> > >>>>> pattern.match('dogman').start()
> > >> 0
> > >>>>> pattern.search('dogcatcher').start()
> > >
> > >
> > >
> > > Hi Mike,
> > >
> > > Gaaah, bad copy-and-paste.  The example with 'dogcatcher' actually does
> > > come up with a result:
> > >
> > > ###
> > >>>> pattern.search('dogcatcher').start()
> > > 6
> > > ###
> > >
> > > Sorry about that!
> > >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> --
> 'There is only one basic human right, and that is to do as you damn well please.
> And with it comes the only basic human duty, to take the consequences.
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list