Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex Question

Status
Not open for further replies.

AcidHawk

Technical User
Mar 14, 2001
17
0
0
ZA
Hi,

I am having difficulty groking this regex stuff. I have a string:
"SomeStuff D: Drive is full"

In perl I can do something like if (my $var =~ /\s{1}.:\s/) { ...} where I can check the string to see if there is any occurance of space-anyletter-colon-space

I am trying to do the same in java.. I have...

Pattern p1 = Pattern.compile("\\s\\w:\\s");
Matcher m1 = p1.matcher(myString);
if (m1.matches()) {
System.out.println("MATCH FOUND:" + m1.group(1));
System.out.println("MATCH FOUND:" + m1.group(2));
}
else {
System.out.println("NO MATCH FOUND");
}

This does NOT work.. Can any-one tell me where I am going wrong?

In group(1) I am looking for "SomeStuff" and in group(2) I am looking for "D: Drive is full"

Many thanks
AcidHawk



----
Of All the things I've lost in my life it's my mind I miss the most.
 
AcidHawk :

please do not crosspost (Sun Java Workshop).

I am not personally a regex man, but stefanwagner is I believe - he should be along the forums in a while I would imagine.
 
Hi AcidHawk,

I had a similar problem where the matches() method doesn't seem to do what you would expect. So I ended up doing something like:

Pattern p1 = Pattern.compile("\\s\\w:\\s");
Matcher m1 = p1.matcher(myString);
boolean b1 = m1.find();
if (b1) {
String[] strArr = p1.split(myString);
System.out.println(strArr[0]);
System.out.println(strArr[1]);
}
else {
System.out.println("NO MATCH FOUND");
}

I know this is not ideal but I hope it helps

Nick
 
I would think you're escaping the notations here with the Pattern p1 = Pattern.compile("\\s\\w:\\s");

java should not have a issue with \s\w:\s

also if you are matching ANY occurances then use the [ ]

but if you are explicitly matching a drive then I would think

^\s(\w:)\s$

___________________________________________________________________
[sub]
The answer to your ??'s may be closer then you think. faq333-3811
Join the Northern Illinois/Southern Wisconsin members in Forum1064
[/sub]
 
Here I am :)
Code:
String s = "SomeStuff D: Drive is full";
Pattern p1 = Pattern.compile("(.*) (.: .*)");
Matcher m1 = p1.matcher (s);
if (m1.matches()) 
{
	System.out.println("MATCH FOUND:" + m1.group(1));
	System.out.println("MATCH FOUND:" + m1.group(2));
}

I'm not used to \\s and \\w - they might work too.

onpnt: \s isn't a regular escape-sequence. Therefore you have to mask the \ with another \.

seeking a job as java-programmer in Berlin:
 
aaah - forgot to add some prosa:
To create a capturing group, you use ().
(Therefore they have to be escaped, if meant itself.)

Instead of 'Matcher, Pattern' used explicitly, you could do
Code:
System.out.println (s.replaceAll ("(.*) (.: .*)", "MATCH FOUND: $1 \nMATCH FOUND: $2"));

seeking a job as java-programmer in Berlin:
 
Hey stefanwagner

I know \s isn't a escape-sequence. It is the special sequence for a whitespace. After doing some reminder reading (been a bit) I see that you do need the double up in java (and C# as I just found) unless you add the @.


I'm just crossing language syntax again. I'll keep it shut until I'm sure from now on ;)


___________________________________________________________________
[sub]
The answer to your ??'s may be closer then you think. faq333-3811
Join the Northern Illinois/Southern Wisconsin members in Forum1064
[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top