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();
 
R

Roedy Green

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
 
R

Roedy Green

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 ™ 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
 
R

Roedy Green

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
}
}

--
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
 
R

Roedy Green

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
}



--
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
 

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

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top