Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Pattern Matching - groupcount

  • 16-07-2007 04:00PM
    #1
    Registered Users, Registered Users 2 Posts: 1,559 ✭✭✭


    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, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil




  • Registered Users, Registered Users 2 Posts: 378 ✭✭sicruise


    Are we not supposed to be an alternative to google?


  • Registered Users, Registered Users 2, Paid Member Posts: 2,427 ✭✭✭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