¿Cómo se relaciona más de un carácter de espacio en Java regex?Java Regex: cómo hacer coincidir uno o más caracteres de espacio
Tengo una expresión regular que estoy tratando de hacer coincidir. La expresión regular falla cuando tengo dos o más caracteres espaciales.
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();
}
'String $ pattern' =>' Patrón de cadena' en las convenciones de codificación estándar de Java. – assylias
¿Es eso ... la sintaxis php en código Java? – ean5533
Podría ser que el segundo espacio coincida con [^ a] (un espacio no es una 'a') – erikxiv