Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Pattern Matching - groupcount

Options
  • 16-07-2007 4:00pm
    #1
    Registered Users Posts: 1,552 ✭✭✭


    Im trying to figure out how to use patterns and matcher.

    I don't understand how groupCount works.

    How does it work?

    Are there any good explanations about?


Comments

  • Closed Accounts Posts: 50 ✭✭Farouk.Bulsara


    quinnd6 wrote:
    Im trying to figure out how to use patterns and matcher.

    I don't understand how groupCount works.

    How does it work?

    Are there any good explanations about?


    http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.html#groupCount()


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil




  • Registered Users Posts: 378 ✭✭sicruise


    Are we not supposed to be an alternative to google?


  • Registered Users Posts: 2,426 ✭✭✭ressem


    How about
    http://java.sun.com/docs/books/tutorial/essential/regex/intro.html

    Do you understand what groups are, in the context of Matcher etc.

    Take a configuration file with a line that looks like:
    clients: 192.168.1.50
    192.168.200.52 MyComputer optional description
    10.168.7.63

    amongst other stuff. And you want to retrieve these ip addresses.
    You might write a regex like
    String clientregex =
    "^clients:\D*
    (
    (\d{1,3}\\.\d{1,3}\\.\d{1,3}\\.\d{1,3})
    (.*?)??$)
    ){1,}?"

    2nd and 5th deal with allowing multiple addresses
    3nd line is the ip address.
    4rd line is the description

    Above not guaranteed to work as it's a friday but anyways.
    Each ( ...stuff.... ) Creates a group.

    So above there's
    group 0 which will match the entire expression being tested. Not included in count
    group 1 should match the first ip + description (if it existed).
    group 2 should match the first ip
    if there was a description then this would be group 3, but there isn't so group 3 is the second ip + description.
    group 4 is the second ip
    group 5 is the second ip's description
    group 6 is the third ip + description
    group 7 is the third ip

    so groupcount will equal 7 with this data.

    So it can be used to iterate between the above values like.
    for (int i=1; i<mymatcher.groupcount(); i++)
    { value= mymatcher.group(i);}


Advertisement