Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Help with regexp pattern

Thread view: 
- - 04 Jul 2005 03:06 GMT
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();
Roedy Green - 04 Jul 2005 03:47 GMT
>I'm having trouble parsing a string with the following formats using
>regular expression:
[quoted text clipped - 21 lines]
>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.

Signature

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 - 04 Jul 2005 04:05 GMT
>Format 1:
>String string1 = ":prefix COMMAND param1 param2";
[quoted text clipped - 18 lines]
>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 ™ 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

      }
  }

Signature

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 - 04 Jul 2005 04:06 GMT
>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 ™ 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
     }
  }

Signature

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 - 04 Jul 2005 04:43 GMT
>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 ™ 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
  }

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.