Java Regex: how to combine one or more space characters

How do you match more than one space character in Java regex?

I have a regex that I'm trying to match. Re-expression fails when I have two or more spaces.

public static void main(String[] args) { String pattern = "\\b(fruit)\\s+([^a]+\\w+)\\b"; //Match 'fruit' not followed by a word that begins with 'a' String str = "fruit apple"; //One space character will not be matched String str_fail = "fruit apple"; //Two space characters will be matched System.out.println(preg_match(pattern,str)); //False (Thats what I want) System.out.println(preg_match(pattern,str_fail)); //True (Regex fail) } public static boolean preg_match(String pattern,String subject) { Pattern regex = Pattern.compile(pattern); Matcher regexMatcher = regex.matcher(subject); return regexMatcher.find(); } 
+6
source share
1 answer

The problem is actually related to backtracking . Your regex:

  "\\b(fruit)\\s+([^a]+\\w+)\\b" 

Says “fruit”, followed by one or more spaces, followed by one or more “not” characters, followed by one or more “word characters”. The reason this fails with two spaces is because \s+ matches the first space, but then returns the second, which then satisfies [^a]+ (with the second space) and the part \s+ (with the first).

I think you can fix this by simply using the posessive quantum instead, which will be \s++ . This says \s not to return a second space. You can find the documentation on Java quantifiers here .


To illustrate, here are two examples in Rubular:

+12
source

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


All Articles