PDA

View Full Version : Partial matching of regex



ehamberg
28th May 2008, 18:16
Hello,

I have a problem I'm trying to find a good solution for and I need some help.
Let's say I have the regex QRegExp myRegex("x.y[0-5]z"). I also have a string that grows one character at the time.
What I need to find out is if the string matches the regex, or if not: if it can match in the future, i.e. after adding more characters.

Basically I want this:

canMatch("x", myRegex); // true
canMatch("xy", myRegex); // true
canMatch("xyy", myRegex); // true
canMatch("xyyy", myRegex); // false
canMatch("xyy4", myRegex); // true
canMatch("xyy6", myRegex); // false
canMatch("xyy4z", myRegex); // true

Does anyone have any ideas? The only regex implementation I have available is QRegExp.

ehamberg
28th May 2008, 20:13
*drum roll*

By using exactMatch(), one can query matchedLength() afterwards and check if that's the same length as the string. Voila!