关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。
想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。
7年前关闭。
Improve this question
我正在尝试使用正则表达式和正则表达式测试电话号码格式,当我运行 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;
}
是字符串
(\\([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})");
原样:
( - start of mathing group
\\( - matches (
[0-9]{3} - 3 digits
\\) - matches )
) - end of matching group (this is the one you missed)
关于java - 使用正则表达式在索引附近的未封闭组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22901462/