regular expression reverse match?
Ron Adam
radam2 at tampabay.rr.com
Wed Oct 29 03:15:20 EST 2003
On Tue, 28 Oct 2003 20:09:38 -0800, "Emile van Sebille"
<emile at fenx.com> wrote:
>I'll assume you mean comething like:
>
>x = re.compile('abcd and a bunch of other stuff')
>
>>
>> This is what i'm looking for:
>>
>> string / result
>> 'a' / pass
>> 'ab' / pass
>> 'abc' / pass
>> 'abd' / fail
>> 'aaaa' / fail
>> 'abcd and a bunch of other stuff and then some' / fail
>>
>> Is there a way to form a regular expression that will do this?
>
>for k,v in re._cache.items():
> if v == x:
> ss=k[0]
> break
>
>Then it's a normal:
>
>>>> re.match('a',ss)
><_sre.SRE_Match object at 0x009828E0>
>>>> re.match('abd',ss)
>>>>
>
Hi again Emile,
I tried it and get an error.
Here's my result. Am I missing something?
>>>
>>> import re
>>> x = re.compile('abcd and a bunch of other stuff')
>>> for k,v in re._cache.items():
if v==x:
ss=k[0]
break
Traceback (most recent call last):
File "<pyshell#21>", line 1, in ?
for k,v in re._cache.items():
AttributeError: 'module' object has no attribute '_cache'
>>>
With a trial and error method, (works for me eventually), this is what
I've been able to get to work so far. I found the '$' is what I
needed to limit the buffer length to the pattern.
e = kb_input('Enter "Y" or "N" please: ', '[YyNn]$')
f = kb_input('Enter "yes" please: ', 'y$|ye$|yes$')
g = kb_input('Enter "yes" or "no": ', '(y$|ye$|yes$)|(n$|no$)')
h = kb_input('Enter a 5 digit number:','\d$|\d{2}$\d{3}$\d{4}$\d{5}$')
New problem: Is there a way to expand an re from:
'yes$' to 'y$|ye$|yes$'
and
'(yes$)|(no$)' to '(y$|ye$|yes$)|(n$|no$)'
and
'\d{30}$' to '\d{1}$|\d{2}$|\d{3}$|\d{4}$|\d{5}$| .......'
Other expressions that I might use would be:
'\d{3}-\d{3}-d\{4}$' to match a phone number
or '\c{40}' to specify a character string 40 characters long.
but if I have to manually expend these to the above formats they can
get pretty long.
''abcd and a bunch of other stuff' becomes...
'a$|ab$|abc$|abcd$|abcd $|abcd a$|abc... etc... etc... ....stuff$'
Well at least I know what the re's look like now. Time to sleep on it
and see what tomorrow brings.
_Ron
More information about the Python-list
mailing list