Results 1 to 1 of 1
  1. #1
    johann_p is offline Member
    Join Date
    Jun 2007
    Rep Power

    Default Get all groups from a regular expression match?

    Please help me understand how to use Java regular expressions:

    import java.util.regex.Pattern;
    import java.util.regex.Matcher;

    class RegExp1 {
    public static void main(String[] args) {
    String testString = "first|aaaa | bbbb\n|cccc|ddddd";
    Pattern pattern = Pattern.compile("^([^|]+)(\\|[^|]*)+$");
    Matcher matcher = pattern.matcher(testString);
    int groupcount = matcher.groupCount();
    System.out.println("Found "+groupcount+" groups");
    System.out.println("Matcher: "+matcher);
    for (int i = 1; i <= groupcount; i++) {
    System.out.println("Match "+i+": "+testString.substring(matcher.start(i),matcher.en d(i)));
    I will get "first" for Match 1 and "|ddddd" for Match 2, but how do I get the other matches for the second capturing group?
    Last edited by johann_p; 05-16-2008 at 08:31 PM.

Similar Threads

  1. Complex Regular Expression HELP
    By hiklior in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 01:52 PM
  2. Regular Expression Challange
    By hiklior in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 05:05 AM
  3. regular expression for unicode
    By tharhan in forum Advanced Java
    Replies: 0
    Last Post: 04-01-2008, 10:53 PM
  4. Capturing Groups using regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 12:19 PM
  5. I can't find the right regular expression
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:36 AM

Posting Permissions

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