[Tutor] Regular expression - I

Steve Willoughby steve at alchemy.com
Tue Feb 18 19:20:09 CET 2014


The problem is not the use of the raw string, but rather the regular expression inside it.

In regular expressions, the * means that whatever appears before it may be repeated zero or more times.  So if you say H* that means zero or more H’s in a row.  I think you mean an H followed by any number of other characters which would be H.*  (the . matches any single character, so .* means zero or more of any characters).

On the other hand, H\* means to match an H followed by a literal asterisk character.

Does that help clarify why one matched and the other doesn’t?

steve

On 18-Feb-2014, at 10:09, Santosh Kumar <rhce.san at gmail.com> wrote:

> Steve,
> 
> i am trying to under r - raw string notation. Am i understanding it wrong.
> Rather than using "\", it says we can use the "r" option.
> 
> http://docs.python.org/2/library/re.html
> 
> Check the first paragraph for the above link.
> 
> Thanks,
> santosh
> 
> 
> 
> On Tue, Feb 18, 2014 at 11:33 PM, Steve Willoughby <steve at alchemy.com> wrote:
> Because the regular expression <H*> means “match an angle-bracket character, zero or more H characters, followed by a close angle-bracket character” and your string does not match that pattern.
> 
> This is why it’s best to check that the match succeeded before going ahead to call group() on the result (since in this case there is no result).
> 
> 
> On 18-Feb-2014, at 09:52, Santosh Kumar <rhce.san at gmail.com> wrote:
> 
> >
> > Hi All,
> >
> > If you notice the below example, case I is working as expected.
> >
> > Case I:
> > In [41]: string = "<H*>test<H*>"
> >
> > In [42]: re.match('<H\*>',string).group()
> > Out[42]: '<H*>'
> >
> > But why is the raw string 'r' not working as expected ?
> >
> > Case II:
> >
> > In [43]: re.match(r'<H*>',string).group()
> > ---------------------------------------------------------------------------
> > AttributeError                            Traceback (most recent call last)
> > <ipython-input-43-d66b47f01f1c> in <module>()
> > ----> 1 re.match(r'<H*>',string).group()
> >
> > AttributeError: 'NoneType' object has no attribute 'group'
> >
> > In [44]: re.match(r'<H*>',string)
> >
> >
> >
> > Thanks,
> > santosh
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> 
> -- 
> D. Santosh Kumar
> RHCE | SCSA 
> +91-9703206361
> 
> 
> Every task has a unpleasant side .. But you must focus on the end result you are producing.
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140218/42b211d1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.python.org/pipermail/tutor/attachments/20140218/42b211d1/attachment.sig>


More information about the Tutor mailing list