did*_*ido 0 java regex regex-negation
I have the following regex expression (code below), however I am very confused as to why it does not return 'Banas' since it is a word and is between 2 and 20 characters.
Pattern p = Pattern.compile("\\s[A-Za-z]{2,20}\\s");
Matcher m = p.matcher(" Derek Banas CA 1234 PA (750)555-1234");
while(m.find()){
System.out.println(m.group());
}
Run Code Online (Sandbox Code Playgroud)
The output is below. Why is "Banas" not in the output? Thanks.
Derek CA PA
Run Code Online (Sandbox Code Playgroud)
Because first match consumed space after Derek so Banas cant use it at start. Try maybe changing your regex to "\\b[A-Za-z]{2,20}\\b". \\b is word boundary which will match only places that are
- before the first character in the string, if the first character is a word character.
- after the last character in the string, if the last character is a word character.
- between two characters in the string, where one is a word character and the other is not a word character.
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |