[Tutor] regex help

spir denis.spir at free.fr
Mon Feb 23 14:18:14 CET 2009


Le Mon, 23 Feb 2009 06:45:23 -0500,
Kent Johnson <kent37 at tds.net> s'exprima ainsi:

> On Sun, Feb 22, 2009 at 10:49 PM, ish_ling <ish_ling at yahoo.com> wrote:
> > I have a string:
> >
> >    'a b c<H d e f gH> h<H i j kH>'
> >
> > I would like a regex to recursively match all alpha letters that are
> > between <H and [a-z]H>. That is, I would like the following list of
> > matches:
> >
> >    ['d', 'e', 'f', 'i', 'j']
> >
> > I do not want the 'g' or the 'k' matched.
> >
> > I have figured out how to do this in a multiple-step process, but I would
> > like to do it in one step using only one regex (if possible). My multiple
> > step process is first to use the regex
> >
> >    '(?<=H )[a-z][^H]+(?!H)'
> 
> I would use a slightly different regex, it seems more explicit to me.
> r'<H([a-z ]+?)[a-z]H>'

You can even probably exclude leading & trailing spaces from match:
r'<H ([a-z ]+?) [a-z]H>'
returns
['d e f', 'i j']


> Kent
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


------
la vita e estrany


More information about the Tutor mailing list