Java regular expression with hyphen

I need to correlate and analyze the data in a file that looks like this:

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

but the template that I wrote below does not work. Can someone help me understand why?

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");
    }
}
+5
source share
4 answers

It looks right. There is probably something strange in your lines. Find extra spaces and line breaks.

Try the following:

final Matcher m = p.matcher(this.currentLine.trim());
+7
source

You tried to avoid -how \\-?

+4
source

. , , . :

final String patternStr = "(\\d{4})-(\\d{1})-(\\d{2})-(\\d{3})-(\\d{1})-(\\d{6})";
+3

 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

final String patternStr = "\\s*(\\d+)-(\\d+)-(\\d+)-(\\d+)-(\\d+)-(\\d+)";
+1

All Articles