Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex group 0

Tags:

java

regex

Can someone please help me to explain the following code? Thanks. I am a little bit confused about the regex grouping.

public static String evaluate(String s) {
    if (s == null) return "";

    Matcher m = Pattern.compile("[0-9]*").matcher(s);

    if (m.find()) {
        MatchResult mr = m.toMatchResult();
        return mr.group(0);
    }
    return "";
}
like image 585
user1653240 Avatar asked Jan 17 '13 18:01

user1653240


People also ask

What is group 0 regex?

group(0) is entire match, group(1) is match inside first set of (...) in your regex, and so on.

WHAT IS group in Java regex?

Java Prime Pack Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".

What is group () in Java?

Java Matcher group() Method The group method returns the matched input sequence captured by the previous match in the form of the string. This method returns the empty string when the pattern successfully matches the empty string in the input.

How do I match a group in regex?

Regular expressions allow us to not just match text but also to extract information for further processing. This is done by defining groups of characters and capturing them using the special parentheses ( and ) metacharacters. Any subpattern inside a pair of parentheses will be captured as a group.


2 Answers

Hope this makes the group 0 more clear:

Example:

    String str = "start123456end"; // Your input String


    //                                 Group#1 Group#2
    //                                   |      |  
    Pattern p = Pattern.compile("start([0-9]*)(end)");
    //                           |<--- Group#0 --->|


    Matcher m = p.matcher(str); // Create a matcher for regex and input

    while( m.find() ) // As long as your regex matches something
    {
        System.out.println("group#0:\t" + m.group()); // Or: m.group(0)
        System.out.println("group#1:\t" + m.group(1));
        System.out.println("group#2:\t" + m.group(2));
    }

Output:

group#0:    start123456end
group#1:    123456
group#2:    end

You can "store" some parts of your regex into groups. in my example you have 3 of them (groups are between (and )):

  • Group 1: numbers between start and end words.
  • Group 2: the end word only
  • Group 0: thats the whole thing that matches your pattern - group 0 is reserved and will always return the whole match, while all others are optional and defined by you.

According to your code:

Example:

Matcher m = Pattern.compile("[0-9]*").matcher("123456end"); // Matches all numbers

if( m.find() )
{
    System.out.println(m.group(0)); // m.group() possible too
}

There only one group: 0!

Output: 123456 (= group 0)

now lets put some more groups into the pattern:

Code:

    //                            Group#1      Group#2
    //                              |            |
    Matcher m = Pattern.compile("([0-9])[0-9]([0-9])*").matcher(str); // Matches all numbers
    //                           |<---- Group#0 ---->|

    if( m.find() )
    {
        System.out.println("group#0:\t" + m.group(0)); // m.group() possible too
        System.out.println("group#1:\t" + m.group(1)); // 1st digit
        System.out.println("group#2:\t" + m.group(2)); // 3rd digit
    }

There are two more groups now.

Output:

group#0:    123456
group#1:    1
group#2:    6

I recommend you this documentation: Lesson: Regular Expressions. Realy start from first chapter and try examples by your own.

Additional:

  • Java Regex Tutorial (see 3.4. Grouping and Backreference for grouping)
  • Guide to Regular Expressions in Java (Part 1)
  • Guide to Regular Expressions in Java (Part 2)
like image 128
ollo Avatar answered Nov 15 '22 22:11

ollo


From the documentation:

Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group().

In other words, mr.group(0) is the entire match.

like image 42
NPE Avatar answered Nov 16 '22 00:11

NPE