Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Help with regexp pattern (http://www.velocityreviews.com/forums/t144614-help-with-regexp-pattern.html)

- 07-04-2005 02:06 AM

Help with regexp pattern
 
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 07-04-2005 02:47 AM

Re: Help with regexp pattern
 
On Mon, 04 Jul 2005 10:06:34 +0800, - <nobody@hoem.com> 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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Roedy Green 07-04-2005 03:05 AM

Re: Help with regexp pattern
 
On Mon, 04 Jul 2005 10:06:34 +0800, - <nobody@hoem.com> 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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Roedy Green 07-04-2005 03:06 AM

Re: Help with regexp pattern
 
On Mon, 04 Jul 2005 10:06:34 +0800, - <nobody@hoem.com> 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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Roedy Green 07-04-2005 03:43 AM

Re: Help with regexp pattern
 
On Mon, 04 Jul 2005 10:06:34 +0800, - <nobody@hoem.com> 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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes


All times are GMT. The time now is 04:17 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.