YANRQ (yet another regex question)

Tim Peters tim.one at comcast.net
Thu Mar 14 22:37:54 EST 2002


>> Depending on the definition of easy, this is the alternative I would
>> use.
>>
>> 	r = re.compile(r'(?P<foo>(?=.{7}) *foo *)')
>>
>> Of course, your milage may wary.

[Michael George Lerner]
> Doesn't quite work for me:
>
> >>> import re
> >>> r = re.compile(r'(?P<foo>(?=.{7}) *foo *)')
> >>> s = r.match('foo         ').groupdict()['foo']
> >>> s
> 'foo         '
> >>> len(s)
> 12
> >>>
>
> and I only want to match things that are seven characters long here.

If matches, e.g. 'foobarx' too.

> It does, however, match all of the correct seven character strings,
> and I'll have to remember that trick in the future.  Thanks!

If you can settle for a regexp that matches *at least* the correct strings,
'' would work too <wink>.





More information about the Python-list mailing list