regular expression reverse match?
Emile van Sebille
emile at fenx.com
Tue Oct 28 23:09:38 EST 2003
"Ron Adam" <radam2 at tampabay.rr.com> wrote in message
news:fqcupvkic63mrb5hv1hbn1g06fggkdl91g at 4ax.com...
>
> Is it possible to match a string to regular expression pattern instead
> of the other way around?
You can abuse the implmentation details to discover the original search
string.
>
> For example, instead of finding a match within a string, I want to
> find out, (pass or fail), if a string is a partial match to an re.
>
> Given an re of 'abcd and a bunch of other stuff'
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)
>>>
Not sure that's what you're looking for, but reasonably sure it won't work
in all cases.
HTH,
Emile van Sebille
emile at fenx.com
More information about the Python-list
mailing list