Regular Expression Newbie question

L

Lee

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
 
K

Knute Johnson

Lee said:
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));
}
 
T

Tor Iver Wilhelmsen

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".
 
L

Lee

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
 
J

John C. Bollinger

Lee said:
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 address removed)
 
A

Alan Moore

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top