Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > regex group(1) problem

Reply
Thread Tools

regex group(1) problem

 
 
bahhab@hotmail.com
Guest
Posts: n/a
 
      05-28-2005
Hi

The first code snip works but takes 4 lines and as there will be a
number of pattern matches I'd rather do each on one line. The second
snip is how I think it should work except I get "cannot resolve symbol
symbol : variable matcher". Is this possible and if so what should my
variable "matcher" be?

<snip>
Pattern regex = Pattern.compile("^(LRC[0-9]{5}).*");
Matcher match = regex.matcher(line);
boolean b = match.matches();
if (b) {id [i] = match.group(1);}
<snip>

<snip2>
if (Pattern.compile("^(LRC[0-9]{5}).*").matcher(line).matches()) { /
id [i]= matcher.group(1);}
<snip2>

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      05-29-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>Hi
>
>The first code snip works but takes 4 lines and as there will be a
>number of pattern matches I'd rather do each on one line. The second
>snip is how I think it should work except I get "cannot resolve symbol
>symbol : variable matcher". Is this possible and if so what should my
>variable "matcher" be?
>
><snip>
>Pattern regex = Pattern.compile("^(LRC[0-9]{5}).*");
>Matcher match = regex.matcher(line);
>boolean b = match.matches();
>if (b) {id [i] = match.group(1);}
><snip>
>
><snip2>
>if (Pattern.compile("^(LRC[0-9]{5}).*").matcher(line).matches()) { /
>id [i]= matcher.group(1);}
><snip2>
>
>
>

In your <snip2> code, Matcher object is unnamed.
 
Reply With Quote
 
 
 
 
Filip Larsen
Guest
Posts: n/a
 
      05-29-2005
(E-Mail Removed) wrote

> Pattern regex = Pattern.compile("^(LRC[0-9]{5}).*");
> Matcher match = regex.matcher(line);
> boolean b = match.matches();
> if (b) {id [i] = match.group(1);}


If you want to compact the code you can replace the variables that are
only referenced once (regex and b) with their initializer value:

Matcher matcher = Pattern.compile("^(LRC[0-9]{5}).*").matcher(line);
if (matcher.matches()) id[i] = matcher.group(1);


Regards,
--
Filip Larsen


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments