[Tutor] need help with a regular expression

Lie Ryan lie.1296 at gmail.com
Sat Jun 28 18:09:40 CEST 2008


On Sat, 2008-06-28 at 17:39 +0200, Andre Engels wrote:
> On Sat, Jun 28, 2008 at 3:21 PM, Lie Ryan <lie.1296 at gmail.com> wrote:
> 
> > > I think
> > >
> > > _{0,3}[A-Z](\-?[A-Z0-9])+
> > >
> > > will do what you are looking for.
> >
> > That, doesn't allow single hyphen, which his requirement allowed as long
> > as it (the hypehn) is not as the first or last character.
> 
> The \-? allows a hyphen, doesn't it?
> 

Oh, right... when I tested it, I was using small caps, which is why it
doesn't match. But it do have a curious behavior with double dash, when
it met double underscore it cuts the matched string:

>>> s = '__ABC--DE'
>>> pat.match(s).group()
'__ABC'

I think it's because __ABC do match the required pattern. And match's
behavior is to check whether the beginning of the string matches the
pattern, it doesn't care if there is extra character after that.




More information about the Tutor mailing list