From: Daniel Brockman Date: 2005-07-01T17:23:06+09:00 Subject: Re: Ask for help about Regexp Nikolai Weibull writes: > Eric Luo wrote: > >> I need to match my input dynamically by Regexp, For example: I want >> match to this regexp: mask = /\d{10}tt\d{0, 5} then I give my input >> byte by byte. And I want to be noticed at the first time when it is >> impossible for me to match my input to the regexp mask. so if i input >> 34789d, when I the input the character 'd', I will be noticed. >> >> I wonder if it's possible or not. if it is, how could I do that? > > Well, you can’t easily do that. You could write a regex that matches > valid prefixes of the strings of the language of the final regex, but > that can become quite complicated. You should probably rethink your > algorithm, Nikolai is right: you can't easily do that. But I think it would be easy to modify the regular expression engine to make it possible. Unless I am mistaken, the only information you need is whether or not the engine ever wanted to look past the end of the input string. If the engine ever managed to consume all characters and still be hungry for more, then your string is a valid prefix. Conversely, if the engine did not do this, then your string is not a valid prefix. Note that merely consuming all characters is not sufficient; the match has to fail due to lack of additional input. I just posted about this on Perl Monks, so consider following up there if you have anything to add. I don't feel like hacking this into regex.c right now, and I'm not sure what the API should be like. But I do think it would be a useful feature. The fact that noone seems to have wanted it before baffles me. Perhaps up until now, noone even considered the possibility. -- Daniel Brockman