YANRQ (yet another regex question)

Michael George Lerner mlerner at asteroids.gpcc.itd.umich.edu
Thu Mar 14 12:45:51 EST 2002


Tim Peters <tim.one at comcast.net> wrote:
> [Michael George Lerner]

<snip>

>> I'd like to rewrite my regular expression to match any of these,
>> and I'd really rather not write it all out like this:
>>
>> (?P<foo>(foo    | foo   |  foo  |   foo |    foo|)

> That will also match an empty string (remove the last vertical bar; ditto
> the 2nd left paren).

Oops.  That's what I get for retyping things quickly instead of cut 'n
pasting them.

>> Is there some easy way to do this that I've overlooked?

> You already found an easy way <wink>.  If you ask whether there's an easier
> way, the answer is no.

hrmph.  oh well.  at least i can save myself some time with this:

>>> def getPat(text, patLength, padChar = ' ', patName = None):
... 	text = text.strip()
... 	if patName is None:
... 		patName = text
... 	assert len(text) <= patLength
... 	numPadChars = patLength - len(text)
... 	pats = []
... 	for i in range(numPadChars + 1):
... 		pats.append(padChar * i + text + padChar * (numPadChars - i))
... 	return r'(?P<%s>%s)' % (patName, '|'.join(pats))
... 		
>>> getPat('foo',7)
'(?P<foo>foo    | foo   |  foo  |   foo |    foo)'
>>> getPat('foo',7,'.')
'(?P<foo>foo....|.foo...|..foo..|...foo.|....foo)'
>>> getPat('foo',7,'.','flonk')
'(?P<flonk>foo....|.foo...|..foo..|...foo.|....foo)'
>>> getPat('foo',2)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 5, in getPat
AssertionError
>>> r = re.compile(getPat('foo',7))
>>> len(r.match('  foo              ').groupdict()['foo'])
7
>>> 


-michael




More information about the Python-list mailing list