[Tutor] Matching with beginning of the line in the character set

Mike Bell ternary at gmail.com
Tue Feb 1 19:58:49 CET 2005


Alternatively, you could .split() your string and not invoke the
mechanisms dealing with regular expressions.

Is this considered stylistically sloppy in the event that the split
results in a very long array?  Sloppier than REs are to begin with?

I think that the problem with [\A\d] is that \A is zero-length, which
doesn't make sense in the context of [].  Brackets aren't shorthand
for "'or' a bunch of small things together" but rather "'or' a bunch
of these single-character matches together"

mike


On Tue, 1 Feb 2005 13:30:44 -0500, Smith, Jeff <jsmith at medplus.com> wrote:
> Kent,
> 
> I think \b will work for me since I was really looking for [\A\W]
> anyway.
> 
> That still doesn't answer the generalized question about something like
> [\A\d] for instance.
> 
> Thanks,
> Jeff
> 
> BTW, I was using raw strings although I forgot to put that in.
> 
> -----Original Message-----
> From: Kent Johnson [mailto:kent37 at tds.net]
> Sent: Tuesday, February 01, 2005 1:15 PM
> Cc: Tutor at python.org
> Subject: Re: [Tutor] Matching with beginning of the line in the
> character set
> 
> Smith, Jeff wrote:
> > I want to match a string which is preceeded by a space or occurs at
> > the beginning of the line.  I also don't want to catch the preceeding
> > character as a group.
> >
> > I have found both of the following to work
> >       re.compile('(?:^|\s)string')
> >       re.compile('(?:\A|\s)string')
> 
> How about r'\bstring' ? It doesn't mean quite the same as \sstring but
> it might work for you.
> 
> >
> > But would prefer to use the more concise [] notation.  However
> >       re.compile('[^\s]string')
> 
> As the first character in [], ^ means 'not'.
> 
> > Does not work and
> >       re.compile('[\A\s]string')
> 
> I guess \A doesn't count as a 'character class'? Or do you need to be
> using raw strings?
> 
> Kent
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list