Regular Expression Newbie question

Discussion in 'Java' started by Lee, Oct 12, 2003.

  1. Lee

    Lee Guest

    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
     
    Lee, Oct 12, 2003
    #1
    1. Advertising

  2. 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...
     
    Knute Johnson, Oct 12, 2003
    #2
    1. Advertising

  3. (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".
     
    Tor Iver Wilhelmsen, Oct 12, 2003
    #3
  4. Lee

    Roedy Green Guest

    On 11 Oct 2003 19:54:46 -0700, (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.
     
    Roedy Green, Oct 12, 2003
    #4
  5. Lee

    Lee Guest

    >
    > 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
     
    Lee, Oct 13, 2003
    #5
  6. 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
     
    John C. Bollinger, Oct 13, 2003
    #6
  7. Lee

    Alan Moore Guest

    On Mon, 13 Oct 2003 09:11:59 -0500, "John C. Bollinger"
    <> 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.
     
    Alan Moore, Oct 14, 2003
    #7
    1. Advertising

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. VSK
    Replies:
    2
    Views:
    2,335
  2. googleboy
    Replies:
    6
    Views:
    341
    Paul McGuire
    Sep 23, 2005
  3. Peter Vanderhaden
    Replies:
    5
    Views:
    138
    SpringFlowers AutumnMoon
    Sep 30, 2007
  4. Sven
    Replies:
    5
    Views:
    100
    Helgi Briem
    Oct 24, 2003
  5. John

    newbie regular expression question

    John, Dec 3, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    154
    Glenn Jackman
    Dec 3, 2003
Loading...

Share This Page