3

I have a String that is incrementally built. While the string being built, it is matched as a whole by a regular expression and when a match is found, a certain task is performed.

My requirement is: If in the middle of the string building process it is found that there is no way an exact match will be found, then the string should be reset and the build process should be re-initiated.

For example if a regular expression is "mada12gaskar" and when a char "3" is added to an existing string "mada1" the string should be cleared and the build process should start over again as "mada13" will never match with "mada12gaskar". Is this possible through Java regex API?

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
jrpalla
  • 159
  • 2
  • 10
  • Provide an example of the code you are trying to write to clarify the requirement. Regex is regex, so the matching should be possible, but you'll need other logic to "clear your String". – eebbesen Jul 21 '13 at 14:42
  • 2
    Why the downvotes? This is quite a good question to me. – JB Nizet Jul 21 '13 at 14:45
  • Im quite not an expert, but why don't you simply store the last matching string (e.g. `mada1`) in an seperate variable which is overwritten with the next matching string? In case the given String (`mada13`) doesn't match, overwrite it with the last matching string, stored seperately.. – nozzleman Jul 21 '13 at 14:53
  • You should add a concrete short example of what you want, because it seems nobody (except Marko Topolnik) understands the question as I understand it. As I understand it, if the regexp is `ab`, a doesn't match, so you continue and add another character: `ac`, and you would like to stop because there's no way adding characters to `ac` will ever match the regexp `ab`. – JB Nizet Jul 21 '13 at 14:57
  • I'm quite interested in the subset of regexp you need to support. Which features do you need? Also, could you phrase your intent like _"I need to build all strings that match a particular regexp."_? Or is it more like _"I need to build a string that is matched by all of these thousand regexpes."_, or simply _"I need to know whether this string could be whole matched by this regexp if we appended something to it."_? – Petr Janeček Jul 21 '13 at 15:15
  • I mean " I need to know whether this string could be whole matched by this regexp if we appended something to it?". I am writing a generic framework where the regular expression is not known and will be configured at the time of implementation. – jrpalla Jul 21 '13 at 15:44

1 Answers1

7

I think I found a possible solution to your problem.

Look at Matcher#hitEnd() method:

Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher.

When this method returns true, then it is possible that more input would have changed the result of the last search.

Now, simply match the regexp against your not-yet-fully-constructed String using a Matcher (obtainable via a Pattern instance) and look at the results:

  • if it matched, you have your winner
  • if it didn't match, look at hitEnd():
    • if it's true, build more String and try again
    • if it's false, the current String could never be matched, you can drop it and start over
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • 2
    Hats off, my answer deleted. – Marko Topolnik Jul 21 '13 at 18:55
  • Just one caveat: "if it's `false`, the current String could never be matched, you can drop it and start over"---actually you should *backtrack* and maybe find a different string with the same prefix. – Marko Topolnik Jul 21 '13 at 20:35
  • @MarkoTopolnik I was equally surprised when I discovered the method. Still, there are some goodies to learn in the `Matcher` class, I don't know what half of the other methods do. About the backtracking - yeah, sure. – Petr Janeček Jul 21 '13 at 21:42
  • Many Thanks!. This seems to be my solution. I will try it. – jrpalla Jul 21 '13 at 22:13