[Tutor] regular expression question

Kent Johnson kent37 at tds.net
Thu Mar 10 00:02:03 CET 2005

Mike Hall wrote:
> 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.

The problem is with your use of sub(), not with |.

By default, re.sub() substitutes *all* matches. If you just want to substitute the first match, 
include  the optional count parameter:

  >>> import re
  >>> s = "X R A Y B E"
  >>> re.sub(r"(A) | (B)", '13', s)
'X R 13Y13 E'
  >>> re.sub(r"(A) | (B)", '13', s, 1)
'X R 13Y B E'

BTW, there is a very handy interactive regex tester that comes with Python. On Windows, it is 
installed at


More information about the Tutor mailing list