Java Regex: Look behind group has no "obvious maximum length",

I have a working service that uses regex sent to it and uses it to fetch a value from a string.

I can create the main method inside the class and debug regex (?<=\\().+?(?=\\){1}) and it works fine.

However, as soon as I deploy it to tomcat for testing remotely, I get the following exception:

 Look-behind group does not have an obvious maximum length near index 19 (?<=\\().+?(?=\\){1}) ^ 

Here is the function to parse the value that is called:

 private String parsePattern(String value, String pattern) { String ret = ""; Matcher m = Pattern.compile(pattern).matcher(value); while (m.find()) { ret = m.group(0); } return ret; } 

What happens, does it cause it to compile in the application, but does not work in webapp?

EDIT:

This is not with any line, but the line being checked is: "(Group Camp Update)"

When called from main , the method returns "Group Camp Update" when called via webapp, it throws an exception.

+4
source share
2 answers

The problem is - again - quoting strings in Java code without quoting when reading through any input.

When you insert the line (?<=\\().+?(?=\\){1}) as follows:

 String s1 = "(?<=\\().+?(?=\\){1})"; System.out.println(s1); 

you get this conclusion

 (?<=\().+?(?=\){1}) 

and this is what the regexp analyzer sees.

But when the same line is read using an InputStream (as an example), nothing changes:

 String s1 = new BufferedReader(new InputStreamReader(System.in)).readLine(); System.out.println(s1); 

will print

 (?<=\\().+?(?=\\){1}) 

This means that {1} refers to the part (?=\\) , and not to the part (?<= .

+7
source

I injected your template into ideone and the result is the same as the Tomcat result:

 System.out.println("aoeu".matches("(?<=\\\\().+?(?=\\\\){1})")); 

Notice how I doubled all backslashes in the string literal so that Pattern.compile gets the string contained in your error message.

+1
source

Source: https://habr.com/ru/post/1483763/


All Articles