我需要检查我的字符串是否遵循特定模式。我的字符串始终以 text_data_
开头,然后跟一个数字,如下所示
text_data_123
text_data_124
text_data_126
text_data_127
text_data_128
现在如何检查我得到的字符串是否符合上述格式:
String value = getData();
// but this doesn't work?
if(value.matches("text_data(?<=_)\\d+")) {
// found a match
}
我做错了什么?
最佳答案
lookbehind 不会断言当前位置之前的内容是 _
。
您不需要为此使用lookbehind。
if(value.matches("text_data_\\d+")) { ... }
关于java - 如何匹配特定格式的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31977625/