我正在尝试使用正则表达式,正则表达式来测试电话号码格式,当我运行Pattern.compile时,我收到错误java.util.regex.PatternSyntaxException:未附近的组索引34
public String checkPhoneNum(String inPhoneNum)
{
Pattern checkRegex = Pattern.compile("(\\([0-9]{3}\\)([0-9]{3}(-)[0-9]{4})");
Matcher regexMatcher = checkRegex.matcher(inPhoneNum);
if(regexMatcher.find())
{
return inPhoneNum;
}
else
return null;
}
Run Code Online (Sandbox Code Playgroud)
是(\\([0-9]{3}\\)([0-9]{3}(-)[0-9]{4})格式(000)111-2222没有正确写的字符串?
您在第一个匹配组中缺少一个右括号:
它应该是
Pattern checkRegex = Pattern.compile("(\\([0-9]{3}\\))([0-9]{3}(-)[0-9]{4})");
Run Code Online (Sandbox Code Playgroud)
原样:
( - start of mathing group
\\( - matches (
[0-9]{3} - 3 digits
\\) - matches )
) - end of matching group (this is the one you missed)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22405 次 |
| 最近记录: |