Come si combina più di un carattere di spazio nell'espressione regolare di Java?Java Regex: come abbinare uno o più caratteri di spazio
Ho una regex che sto cercando di abbinare. La regex ha esito negativo quando ho due o più caratteri di spazio.
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' =>' String pattern' in convenzioni di codifica standard Java. – assylias
È ... sintassi di php nel codice Java? – ean5533
Potrebbe essere che il secondo spazio sia abbinato a [^ a] (uno spazio non è un 'a') – erikxiv