Swagata Ray
Swagata Ray

Reputation: 17

Regex checking runs infinite time

I have written the below regex so that,this regex will not match with the input; It is working but the loop runs infinite time. How to resolve the issue

String originalRegex ="(?s)\\00|\\+ADw-|\\+AD4-|%[0-9a-f]{2}|System[.][a-z]|javascript\\s*:|>(?:\".*|^'.*|[^a-z]'.*|'[^a-z].*|')[-+\\*/%=&|^~\"']|\\?.*<:|\\(\\s*[a-z]{2,}\\.[a-z]{2,}.*\\)";
String xmlData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><configuration xmlns=\"http://www.example.com/api/2.2\" timestamp=\"str111\" version=\"2.2\"><domain account=\"4af17966-c841-4b97-a94a-edd7a0769\" /></configuration>";
String freetext = ">(?:\".*|^'.*|[^a-z]'.*|'[^a-z].*|')[-+\\*/%=&|^~\"']|\\?.*<:";
final Pattern PATTERN_1 = Pattern.compile(freetext);
Matcher matcher = PATTERN_1.matcher(xmlData);

while (!matcher.find()) {
    System.out.println("Good Job");
}

Upvotes: 0

Views: 184

Answers (2)

Prashant Bhate
Prashant Bhate

Reputation: 11087

From the javadocs of Matcher.find()

Attempts to find the next subsequence of the input sequence that matches the pattern.

This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns: true if, and only if, a subsequence of the input sequence matches this matcher's pattern

you should Ideally use

while(matcher.find())

above will try to match input string continuously based on regex pattern.

your while loop will either goes into infinite loop if it is not able to find a pattern or if the match is found it will exit the loop, either way its of no use

while(!matcher.find())

However if you want to check if it doesn't match then you can use if

if(!matcher.find()){
   //No Matches
} else {
   //Atleast one Match
} 

One more tip. If you are trying to parse XML, Regex might not be a right candidate. Try to use one of many XML parsers available.

Upvotes: 2

Adrian Wragg
Adrian Wragg

Reputation: 7401

Java's .find() method returns "true if, and only if, a subsequence of the input sequence matches this matcher's pattern" - see the documentation. In your code, if no matches are found then:

while (!matcher.find()) {
    System.out.println("Good Job");
}

evaluates to:

while (!false) {
    System.out.println("Good Job");
}

or, even simpler:

while (true) {
    System.out.println("Good Job");
}

Thus, your infinite loop.

Upvotes: 4

Related Questions