Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Another Regular Expressions question (http://www.velocityreviews.com/forums/t130933-another-regular-expressions-question.html)

jeffM 02-02-2004 02:02 PM

Another Regular Expressions question
 
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("VCE1246KYTAG001ZX178TAG002DHU7749 TAG003");

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

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

jeff



Hop Pocket 02-02-2004 02:11 PM

Re: Another Regular Expressions question
 
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" <ejbEngine@yahoo.com> wrote in message
news:bvlkve$fok$1@ctb-nnrp2.saix.net...
> 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("VCE1246KYTAG001ZX178TAG002DHU7749 TAG003");
>
> System.out.println( matcher.groupCount() );
>
> and the System.out is printing only 1.Where am I going wrong here?
>
> jeff
>
>




Christophe Vanfleteren 02-02-2004 02:13 PM

Re: Another Regular Expressions question
 
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("VCE1246KYTAG001ZX178TAG002DHU7749 TAG003");
>
> 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

Alan Moore 02-02-2004 11:03 PM

Re: Another Regular Expressions question
 
On Mon, 2 Feb 2004 16:02:23 +0200, "jeffM" <ejbEngine@yahoo.com>
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("VCE1246KYTAG001ZX178TAG002DHU774 9TAG003");
>
>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));
}




All times are GMT. The time now is 01:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.