带连字符的Java正则表达式

Ami*_*ani 5 java regex

我需要在一个看起来像这样的文件中匹配和解析数据:

4801-1-21-652-1-282098
4801-1-21-652-2-282098
4801-1-21-652-3-282098
4801-1-21-652-4-282098
4801-1-21-652-5-282098
Run Code Online (Sandbox Code Playgroud)

但我下面写的模式似乎不起作用.有人可以帮我理解为什么吗?

final String patternStr = "(\\d+)-(\\d+)-(\\d+)-(\\d+)-(\\d+)-(\\d+)";
final Pattern p = Pattern.compile(patternStr);

while ((this.currentLine = this.reader.readLine()) != null) {
    final Matcher m = p.matcher(this.currentLine);
    if (m.matches()) {
        System.out.println("SUCCESS");
    }
}
Run Code Online (Sandbox Code Playgroud)

Rom*_*man 7

看起来很正确.可能是你的行中有些奇怪的东西.寻找一些额外的空间和换行符.

试试这个:

final Matcher m = p.matcher(this.currentLine.trim());
Run Code Online (Sandbox Code Playgroud)


Jas*_*n S 5

您是否尝试过转义-as \\-