regular expression reverse match?
Jay Dorsey
jay at jaydorsey.com
Tue Oct 28 22:19:56 EST 2003
Ron Adam wrote:
> Is it possible to match a string to regular expression pattern instead
> of the other way around?
>
> 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'
>
> 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
>
How about:
>>> matcher = "abcd and a bunch of other stuff"
>>> phrases = ["a", "ab", "abc", "abd", "aaaa", "abcd and a bunch of
other stuff and then some"]
>>> for phrase in phrases:
... if phrase == matcher[:len(phrase)]: print "pass"
... else: print "fail"
...
pass
pass
pass
fail
fail
fail
Jay
More information about the Python-list
mailing list