Results 1 to 2 of 2
Like Tree2Likes
  • 2 Post By JosAH

Thread: Mather groups order

  1. #1
    jammybell is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Mather groups order

    Documentation says:
    Capturing groups are numbered by counting their opening parentheses from left to right.
    but in the code below
    Java Code:
    final String regex = ".*(::.*::).*";
    Pattern pattern = Pattern.compile(regex);
    String query = "::words.txt:: asdf\"::11.txt::\"";
    Matcher matcher = pattern.matcher(query);
    String result = matcher.group(1);
    result variable contains "::11.txt::" (second group), not "::words.txt::" i expect.

    What do I wrong?
    How to capture first inclusion?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Mather groups order

    Try the 'reluctant' quantifier, i.e. make your regular expression ".*?(::.*?::).*". (read the API documentation for the Pattern class for details).

    kind regards,

    Jos
    Fubarable and DarrylBurke like this.
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Pattern problem (capturing groups)
    By BMF in forum New To Java
    Replies: 0
    Last Post: 07-14-2011, 12:52 PM
  2. Groups of swing objects
    By plrca2 in forum AWT / Swing
    Replies: 12
    Last Post: 12-07-2010, 03:56 AM
  3. how to sort jtable with row groups
    By mordjah in forum AWT / Swing
    Replies: 0
    Last Post: 10-25-2010, 05:23 PM
  4. Get all groups from a regular expression match?
    By johann_p in forum New To Java
    Replies: 0
    Last Post: 05-16-2008, 07:50 PM
  5. Capturing Groups using regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •