Tom
Tom

Reputation: 5

Regex pattern format to get the first group

I want to return the string of group 1 in this example, so I have a pattern like below. But it doesn't correct. Can anyone help me to write the pattern please?

    Pattern pattern = Pattern.compile("^(\\w+-\\d)(\\s+)(\\d+)$");

    String line = "list.txt-1   40";
    Matcher list = pattern.matcher(line);

    if(list.matches()) {
        System.out.println("match");
    } else {
        System.out.println("Nope");
    }

Upvotes: 0

Views: 229

Answers (2)

hwnd
hwnd

Reputation: 70722

Your regular expression incorrectly matches the pattern. The regex token \w matches any word character. To match the dot as well, you need to use a character class here.

^([\\w.]+-\\d)(\\s+)(\\d+)$

Also, to return the matched characters captured by Group 1, you need to use find() instead.

String s  = "list.txt-1   40";
Pattern p = Pattern.compile("^([\\w.]+-\\d)\\s+\\d+$");
Matcher m = p.matcher(s);
if (m.find()) {
  System.out.println(m.group(1)); //=> "list.txt-1"
}

Note: You do not need a capturing group around every token unless you want it captured.

But for this example, splitting the string on "one or more" spaces would be easier.

String s = "list.txt-1   40";
String[] parts = s.split(" +");
System.out.println(parts[0]); //=> "list.txt-1"

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

It fails because the above pattern fails to match the .. In-order to match dot, you need to put both . and \\w inside a character class and make it to repeat for one or more times.

Pattern pattern = Pattern.compile("^([\\w.]+-\\d)(\\s+)(\\d+)$");

Example:

String s = "list.txt-1   40";
System.out.println(s.matches("([\\w.]+-\\d)(\\s+)(\\d+)"));

Output:

true

Upvotes: 1

Related Questions