Another Regular Expressions question

Discussion in 'Java' started by jeffM, Feb 2, 2004.

  1. jeffM

    jeffM Guest

    I'm searching for the following substrings TAG001,TAG002 and TAG003 from
    this string

    "VCE1246KYTAG001ZX178TAG002DHU7749TAG003"

    Pattern pattern = Pattern.compile( "'(TAG00\\d[^T]*){3}'" );
    Matcher matcher =
    pattern.matcher("VCE1246KYTAG001ZX178TAG002DHU7749TAG003");

    System.out.println( matcher.groupCount() );

    and the System.out is printing only 1.Where am I going wrong here?

    jeff
     
    jeffM, Feb 2, 2004
    #1
    1. Advertisements

  2. jeffM

    Hop Pocket Guest

    From the java API, matcher.groupCount():

    Returns the number of capturing groups in this matcher's pattern. I.e.
    (TAG00\\d[^T]*). The parentheses denote a capturing group.



    That's just saying that you have one capturing group in your pattern for the
    matcher.
    "jeffM" <> wrote in message
    news:bvlkve$fok$...
    > I'm searching for the following substrings TAG001,TAG002 and TAG003 from
    > this string
    >
    > "VCE1246KYTAG001ZX178TAG002DHU7749TAG003"
    >
    > Pattern pattern = Pattern.compile( "'(TAG00\\d[^T]*){3}'" );
    > Matcher matcher =
    > pattern.matcher("VCE1246KYTAG001ZX178TAG002DHU7749TAG003");
    >
    > System.out.println( matcher.groupCount() );
    >
    > and the System.out is printing only 1.Where am I going wrong here?
    >
    > jeff
    >
    >
     
    Hop Pocket, Feb 2, 2004
    #2
    1. Advertisements

  3. jeffM wrote:

    > I'm searching for the following substrings TAG001,TAG002 and TAG003 from
    > this string
    >
    > "VCE1246KYTAG001ZX178TAG002DHU7749TAG003"
    >
    > Pattern pattern = Pattern.compile( "'(TAG00\\d[^T]*){3}'" );
    > Matcher matcher =
    > pattern.matcher("VCE1246KYTAG001ZX178TAG002DHU7749TAG003");
    >
    > System.out.println( matcher.groupCount() );
    >
    > and the System.out is printing only 1.Where am I going wrong here?
    >
    > jeff


    What you need is matcher.find().

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Feb 2, 2004
    #3
  4. jeffM

    Alan Moore Guest

    On Mon, 2 Feb 2004 16:02:23 +0200, "jeffM" <>
    wrote:

    >I'm searching for the following substrings TAG001,TAG002 and TAG003 from
    >this string
    >
    >"VCE1246KYTAG001ZX178TAG002DHU7749TAG003"
    >
    >Pattern pattern = Pattern.compile( "'(TAG00\\d[^T]*){3}'" );
    >Matcher matcher =
    >pattern.matcher("VCE1246KYTAG001ZX178TAG002DHU7749TAG003");
    >
    >System.out.println( matcher.groupCount() );
    >
    >and the System.out is printing only 1.Where am I going wrong here?
    >
    >jeff
    >


    String str = "VCE1246KYTAG001ZX178TAG002DHU7749TAG003";

    Pattern p = Pattern.compile("TAG00\\d");
    Matcher m = p.matcher(str);

    while (m.find())
    {
    System.out.println(m.group());
    }

    That's how you would find all the matches in the target string. In
    order to apply the pattern to the target, you have to call one of the
    Matcher methods: matches(), lookingAt(), or find(). Once you've done
    that, with a return value of 'true', the group() method returns
    whatever the pattern actually matched. If you have capturing groups
    in your pattern, you can retrieve what they matched by calling
    group(1), group(2), etc. (group(0) is the same as group()). For
    instance, if you just wanted to know what the final digit was in the
    example above, you could do this:

    String str = "VCE1246KYTAG001ZX178TAG002DHU7749TAG003";

    Pattern p = Pattern.compile("TAG00(\\d)");
    Matcher m = p.matcher(str);

    while (m.find())
    {
    System.out.println("Found tag number " + m.group(1));
    }
     
    Alan Moore, Feb 2, 2004
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    801
    Jay Douglas
    Aug 15, 2003
  2. ASP.Confused

    Question on regular expressions

    ASP.Confused, Jul 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    460
    ASP.Confused
    Jul 26, 2004
  3. Replies:
    9
    Views:
    673
    Roedy Green
    Jan 23, 2006
  4. Fred Smith
    Replies:
    1
    Views:
    1,212
    Martin Honnen
    Feb 5, 2004
  5. Anton Ishmurzin

    regular expressions in C - newbie question

    Anton Ishmurzin, Jan 21, 2004, in forum: C Programming
    Replies:
    8
    Views:
    438
    Dan Pop
    Jan 27, 2004
  6. Darren Dale

    question on regular expressions

    Darren Dale, Dec 3, 2004, in forum: Python
    Replies:
    3
    Views:
    345
    Darren Dale
    Dec 3, 2004
  7. Covington Bradshaw
    Replies:
    1
    Views:
    400
    Chris Smith
    Dec 17, 2006
  8. Noman Shapiro
    Replies:
    0
    Views:
    445
    Noman Shapiro
    Jul 17, 2013
Loading...