regular expression reverse match?

Ron Adam radam2 at tampabay.rr.com
Wed Oct 29 00:28:14 EST 2003


On Tue, 28 Oct 2003 20:09:38 -0800, "Emile van Sebille"
<emile at fenx.com> wrote:

>
>"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.
>


I already know the search string... Or will once I understand the how
to form them to do what I want.  <hopefully>   What I don't know is
the completed string that will match to it beforehand.  Or at least
that's the idea.

I'm writing an interactive keyboard input routine and want to use re
to specify the allowed input.  Some things are easy like only allowing
digits or characters.  

So far I can check for %i and %f first to specify simple ints and
floats.  A test cast operation with an exception handles those
perfectly.

As it is, the program checks the input buffer after each keystroke and
determines if the buffer is acceptable against a pattern as the string
is being built.   A reverse match.  It allows new keystrokes to be
added to the buffer as long as it's within the pattern.

I was hoping to use regular expression patterns as a function argument
to specify a wide range of input patterns.

I'm still a little new to Python and a lot new to the regular
expressions although I've used variations of them before. 


So  how do I say....  accept only 1 character out of set [YyNn]  but
only one character and do not match Ye,  yy nn etc...

...  accept  only 10 of any type characters but not 11

... accept  only the letters of "a particular string" sequentially and
then no more.

...  accept a number in the form  of  "nnn-nnn-nnn"  sequentially with
required dashes.

....  accept any sequence of characters and numbers as long as they
contain at least 1 of each type,  cap letter, small letter,  and digit
and be a minimum of 6 characters long.   ie.... a password check.

It's probably easy to do all of these given a completed string first.

This started out as a python learning project..  <grin>  but it's sort
turned into a real interesting coding situation.

    


Thanks for the reply.

_Ron Adam




>>
>> 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)
>>>>
>

I'll give it a try...  not sure either.  Like I said, this is kind of
new to me.  :)


>
>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