Help with regexp pattern

Discussion in 'Java' started by -, Jul 4, 2005.

  1. -

    - Guest

    I'm having trouble parsing a string with the following formats using
    regular expression:


    Format 1:
    String string1 = ":prefix COMMAND param1 param2";

    System.out.println(m.group(0)); ----> :prefix
    System.out.println(m.group(1)); ----> COMMAND
    System.out.println(m.group(2)); ----> param1
    System.out.println(m.group(3)); ----> param2


    Format 2:
    String string2 = "COMMAND param1 param2";

    System.out.println(m.group(0)); ----> null
    System.out.println(m.group(1)); ----> COMMAND
    System.out.println(m.group(2)); ----> param1
    System.out.println(m.group(3)); ----> param2


    This is the pattern i'm using, which obviously isn't right:

    Pattern p =
    Pattern.compile(":)\\S*?\\s*?)((\\S*?\\s*?)(\\s*?\\S*?\\s*?))");
    Matcher m = p.matcher(string1);
    m.matches();
     
    -, Jul 4, 2005
    #1
    1. Advertising

  2. -

    Roedy Green Guest

    On Mon, 04 Jul 2005 10:06:34 +0800, - <> wrote or
    quoted :

    >I'm having trouble parsing a string with the following formats using
    >regular expression:
    >
    >
    >Format 1:
    >String string1 = ":prefix COMMAND param1 param2";
    >
    >System.out.println(m.group(0)); ----> :prefix
    >System.out.println(m.group(1)); ----> COMMAND
    >System.out.println(m.group(2)); ----> param1
    >System.out.println(m.group(3)); ----> param2
    >
    >
    >Format 2:
    >String string2 = "COMMAND param1 param2";
    >
    >System.out.println(m.group(0)); ----> null
    >System.out.println(m.group(1)); ----> COMMAND
    >System.out.println(m.group(2)); ----> param1
    >System.out.println(m.group(3)); ----> param2
    >
    >
    >This is the pattern i'm using, which obviously isn't right:
    >
    >Pattern p =
    > Pattern.compile(":)\\S*?\\s*?)((\\S*?\\s*?)(\\s*?\\S*?\\s*?))");
    >Matcher m = p.matcher(string1);
    >m.matches();


    As Paul Lutus used to say, you have to very clear about what you WANT
    to happen and what IS happening.

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 4, 2005
    #2
    1. Advertising

  3. -

    Roedy Green Guest

    On Mon, 04 Jul 2005 10:06:34 +0800, - <> wrote or
    quoted :

    >
    >
    >Format 1:
    >String string1 = ":prefix COMMAND param1 param2";
    >
    >System.out.println(m.group(0)); ----> :prefix
    >System.out.println(m.group(1)); ----> COMMAND
    >System.out.println(m.group(2)); ----> param1
    >System.out.println(m.group(3)); ----> param2
    >
    >
    >Format 2:
    >String string2 = "COMMAND param1 param2";
    >
    >System.out.println(m.group(0)); ----> null
    >System.out.println(m.group(1)); ----> COMMAND
    >System.out.println(m.group(2)); ----> param1
    >System.out.println(m.group(3)); ----> param2
    >
    >
    >This is the pattern i'm using, which obviously isn't right:
    >
    >Pattern p =
    > Pattern.compile(":)\\S*?\\s*?)((\\S*?\\s*?)(\\s*?\\S*?\\s*?))");
    >Matcher m = p.matcher(string1);
    >m.matches();


    I have converted what you said into a snippet, a self contained little
    program you can test. It behaves differently than you claimed.

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    /**
    * snippet &trade; to demonstrate a problem with regex
    */
    public class Regex1
    {

    private static final Pattern p =
    Pattern.compile(":)\\S*?\\s*?)((\\S*?\\s*?)(\\s*?\\S*?\\s*?))");

    /**
    * test harness
    *
    * @param args not used
    */
    public static void main ( String[] args )
    {

    // format 1
    Matcher m = p.matcher(":prefix COMMAND param1 param2");
    m.matches();

    // genarates
    // Exception in thread "main" java.lang.IllegalStateException:
    No match found
    // at java.util.regex.Matcher.group(Unknown Source)
    // at Regex1.main(Regex1.java:34)


    // claimed // desired
    System.out.println(m.group(0)); // ----> :prefix ??
    System.out.println(m.group(1)); // ----> COMMAND
    System.out.println(m.group(2)); // ----> param1
    System.out.println(m.group(3)); // ----> param2

    // format 2
    p.matcher("COMMAND param1 param2");
    m.matches();
    // claimed // desired
    System.out.println(m.group(0)); // ----> null ??
    System.out.println(m.group(1)); // ----> COMMAND
    System.out.println(m.group(2)); // ----> param1
    System.out.println(m.group(3)); // ----> param2

    }
    }

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 4, 2005
    #3
  4. -

    Roedy Green Guest

    On Mon, 04 Jul 2005 10:06:34 +0800, - <> wrote or
    quoted :

    >I'm having trouble parsing a string with the following formats using
    >regular expression:


    here is what I think you might be trying to do

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    /**
    * snippet &trade; to demonstrate a problem with regex
    * What I think you wanted to do, break the phrase into words.
    * see http://mindprod.com/jgloss/regex.html for more info on
    * splitting, finding and matching.
    */
    public class Regex2
    {

    private static final Pattern p = Pattern.compile(" ");

    /**
    * test harness
    *
    * @param args not used
    */
    public static void main ( String[] args )
    {

    String[] words = p.split(":prefix COMMAND param1 param2");
    for ( String s : words )
    {
    System.out.println(s);
    }
    // :prefix
    // COMMAND
    // param1
    // param2
    //

    words = p.split("COMMAND param1 param2");
    for ( String s : words )
    {
    System.out.println(s);
    }
    // output
    // COMMAND
    // param1
    // param2
    }
    }

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 4, 2005
    #4
  5. -

    Roedy Green Guest

    On Mon, 04 Jul 2005 10:06:34 +0800, - <> wrote or
    quoted :

    >I'm having trouble parsing a string with the following formats using
    >regular expression:


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    /**
    * another possible interpretation of your query

    * snippet &trade; to demonstrate a problem with regex
    * What I think you wanted to do, break the phrase into words,
    stripping out the colon and spaces.
    * see http://mindprod.com/jgloss/regex.html for more info on
    * splitting, finding and matching.
    */
    public class Regex3
    {
    // parse into either 3 or 4 words, if 4 words first must have a
    lead :
    private static final Pattern p =
    Pattern.compile("(\\:(\\S+)\\s+)?(\\S+)\\s+(\\S+)\\s+(\\S+)");


    /**
    * test harness
    *
    * @param args not used
    */
    public static void main ( String[] args )
    {


    Matcher m = p.matcher(":prefix COMMANDA param1 param2");
    if ( m.matches() )
    {
    for ( int i=0; i<=m.groupCount(); i++ )
    {
    System.out.println(m.group(i));
    }
    }

    // both what I expected and what it displays
    // :prefix COMMANDA param1 param2
    // :prefix
    // prefix
    // COMMANDA
    // param1
    // param2

    m = p.matcher("COMMANDB param1 param2");
    if ( m.matches() )
    {
    for ( int i=0; i<=m.groupCount(); i++ )
    {
    System.out.println(m.group(i));
    }
    }

    // both what I expected and what it displays
    // COMMANDB param1 param2
    // null
    // null
    // COMMANDB
    // param1
    // param2

    } // end main
    }



    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 4, 2005
    #5
    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. Greg Hurrell
    Replies:
    4
    Views:
    163
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    490
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  4. Patrick
    Replies:
    4
    Views:
    184
    Markus W.
    Aug 27, 2003
  5. Uldis  Bojars
    Replies:
    2
    Views:
    192
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page