Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regular Expression Newbie question

Reply
Thread Tools

Regular Expression Newbie question

 
 
Lee
Guest
Posts: n/a
 
      10-12-2003
Sorry, I'm used to Perl and am stumbling badly at Java's regular
expressions.

I'm trying to extract strings from withing quotes. IE if a line of
data had this:

"Say something" "Say another thing"
I should get 2 matches with the respective quotes in each. This is
the code I'm using:

String line = "\"Say something\" \"Say another thing\"";
Pattern betweenQuotes = Pattern.compile( "\"(.*?)\"" );
Matcher m = betweenQuotes.matcher( line );
System.out.println( "matches: " + m.matches() );
System.out.println( "count: " + m.groupCount() );
System.out.println( "captured quote: " + m.group(1)) ;

It seems like the non-greedy ? isn't working..
I would expect the text between each quote to show up in m.group(1)
and m.group(2)
What am I missing?

Thank you for whatever help you can give.

Lee
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      10-12-2003
Lee wrote:
> Sorry, I'm used to Perl and am stumbling badly at Java's regular
> expressions.
>
> I'm trying to extract strings from withing quotes. IE if a line of
> data had this:
>
> "Say something" "Say another thing"
> I should get 2 matches with the respective quotes in each. This is
> the code I'm using:
>
> String line = "\"Say something\" \"Say another thing\"";
> Pattern betweenQuotes = Pattern.compile( "\"(.*?)\"" );
> Matcher m = betweenQuotes.matcher( line );
> System.out.println( "matches: " + m.matches() );
> System.out.println( "count: " + m.groupCount() );
> System.out.println( "captured quote: " + m.group(1)) ;
>
> It seems like the non-greedy ? isn't working..
> I would expect the text between each quote to show up in m.group(1)
> and m.group(2)
> What am I missing?
>
> Thank you for whatever help you can give.
>
> Lee


If you want to match your pattern exactly. Pattern betweenQuotes =
Pattern.compile("(\".*\") (\".*\")");

Use "(\".*?\")" and find() if you want to iterate through the double
quoted sections of the string.

String line = "\"Say something\" \"Say another thing\" \"Say the last
thing\"";
System.out.println(line);
Pattern betweenQuotes = Pattern.compile("(\".*?\")");
Matcher m = betweenQuotes.matcher( line );
while (m.find()) {
System.out.println(m.group(1));
}


--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
 
 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      10-12-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Lee) writes:

> System.out.println( "matches: " + m.matches() );


There are three methods of interest in Matcher - you are using the
wrong one. matches() will try to match the whole string, which will
give one "group".
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-12-2003
On 11 Oct 2003 19:54:46 -0700, (E-Mail Removed) (Lee) wrote or
quoted :

>
>"Say something" "Say another thing"
>I should get 2 matches with the respective quotes in each. This is
>the code I'm using:


You need to learn the difference between matching and finding.

See http://mindprod.com/jgloss/regex.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      10-12-2003
>
> There are three methods of interest in Matcher - you are using the
> wrong one. matches() will try to match the whole string, which will
> give one "group".


Thanks to both of you for replying. The find() method nailed it.

Thanks again,

Lee
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      10-13-2003
Lee wrote:

>>There are three methods of interest in Matcher - you are using the
>>wrong one. matches() will try to match the whole string, which will
>>give one "group".

>
>
> Thanks to both of you for replying. The find() method nailed it.


For what it's worth, this seems to be the number one difficulty that
Perl hackers have when learning the Java regex API. I know it bit me
once, and we see questions related to it regularly on this group. I
think it's purely because the meaning of "matches" in Perl jargon is
different from what the Matcher.matches() method does.


John Bollinger
(E-Mail Removed)

 
Reply With Quote
 
Alan Moore
Guest
Posts: n/a
 
      10-14-2003
On Mon, 13 Oct 2003 09:11:59 -0500, "John C. Bollinger"
<(E-Mail Removed)> wrote:
>For what it's worth, this seems to be the number one difficulty that
>Perl hackers have when learning the Java regex API. I know it bit me
>once, and we see questions related to it regularly on this group. I
>think it's purely because the meaning of "matches" in Perl jargon is
>different from what the Matcher.matches() method does.


Not just Perl; every regex implementation seems to define "matches"
the same way, except this one. Sun really screwed up there.
 
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
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments