Page 1 of 1
Pattern in Java not working
Posted: Tue Jul 10, 2007 4:56 pm
by Chris Corbyn
For the two strings:
/home/index.do
/home.do
The following pattern I would assume should work. It works in PHP when I use preg_match(), adding delimiters of course.
Code: Select all
Pattern p = Pattern.compile("^/([^/]+)(/|\\.do$)");
For some reason in Java it successfully matches /home.do, but it never matches /home/index.do. Can anyone see a better pattern possibility? I basically just want to extract the "home" part from it, which of course may not always be "home". I've tried removing the $ but it makes no difference neither.
EDIT | Hmm, even just using the pattern:
^/([^/]+)
On the string
/home/index.do
Fails. Either I need to go to bed, or this makes no sense at all
//Straight off the command-line, hard-coded proof
Code: Select all
chris-corbyns-computer:~/Java d11wtq$ cat Re.java
import java.util.regex.*;
class Re {
public static void main(String[] args) {
Pattern p = Pattern.compile("^/([^/]+)");
Matcher m = p.matcher("/home/index.do");
if (m.matches()) {
System.out.println("It matches");
} else {
System.out.println("NO MATCH");
}
}
}
chris-corbyns-computer:~/Java d11wtq$ javac Re.java
chris-corbyns-computer:~/Java d11wtq$ java Re
NO MATCH
chris-corbyns-computer:~/Java d11wtq$
Posted: Tue Jul 10, 2007 5:12 pm
by Jenk
should that be or '/|\\', or have you double escaped '.' by mistake?
Try (if the former):
Posted: Tue Jul 10, 2007 5:15 pm
by Chris Corbyn
Jenk wrote:should that be or '/|\\', or have you double escaped '.' by mistake?
Try (if the former):
It needs to be a double backslash so that Java doesn't interpret the backslash as an escape. The pattern needs to contain the backslash once Java has compiled the source. I'll try something like you've done, except your pattern would never extract the "home" part which is my objective

Posted: Tue Jul 10, 2007 5:20 pm
by Chris Corbyn
No, wait, I just can't even explain this (no escapes or anything):
Code: Select all
import java.util.regex.*;
class Re {
public static void main(String[] args) {
Pattern p = Pattern.compile("^/([^/]+)");
Matcher m = p.matcher("/home/index.do");
if (m.matches()) {
System.out.println("It matches");
} else {
System.out.println("NO MATCH");
}
}
}
I must have overlooked something in the manual

Posted: Tue Jul 10, 2007 5:24 pm
by feyd
I don't see how preg_match() would match the first unless it does some fuzzy...
Code: Select all
public class pattern
{
public static void main(String[] args)
{
String foo[] = new String[2];
foo[0] = "/home/index.do";
foo[1] = "/index.do";
String d11 = "^/([^/]+)([^/]+\\.do)$";
String pattern = "^/([^/]+/)*([^/]+\\.do)$";
System.out.println("d11's pattern: " + d11);
for(String f : foo)
{
System.out.println(f + " " + (f.matches(d11) ? "does" : "does not") + " match.");
}
System.out.println("\n---------\n");
System.out.println("feyd's pattern: " + pattern);
for(String f : foo)
{
System.out.println(f + " " + (f.matches(pattern) ? "does" : "does not") + " match.");
}
}
}
Code: Select all
d11's pattern: ^/([^/]+)([^/]+\.do)$
/home/index.do does not match.
/index.do does match.
---------
feyd's pattern: ^/([^/]+/)*([^/]+\.do)$
/home/index.do does match.
/index.do does match.
Posted: Tue Jul 10, 2007 5:28 pm
by Chris Corbyn
Looks like you misread my pattern
^/([^/]+)(/|\.do$)
I'm an idiot too

matches() tests the entire string. I need lookingAt().
Thanks for the help anyway

Posted: Tue Jul 10, 2007 5:34 pm
by feyd
Yeah, I forgot the revert a bit of my pattern back to yours.. but even corrected.. it results the same.
Code: Select all
public class pattern
{
public static void main(String[] args)
{
String foo[] = new String[2];
foo[0] = "/home/index.do";
foo[1] = "/index.do";
String d11 = "^/([^/]+)(/|\\.do$)";
String pattern = "^/([^/]+/)*([^/]+\\.do)$";
System.out.println("d11's pattern: " + d11);
for(String f : foo)
{
System.out.println(f + " " + (f.matches(d11) ? "does" : "does not") + " match.");
}
System.out.println("\n---------\n");
System.out.println("feyd's pattern: " + pattern);
for(String f : foo)
{
System.out.println(f + " " + (f.matches(pattern) ? "does" : "does not") + " match.");
}
}
}
Code: Select all
d11's pattern: ^/([^/]+)(/|\.do$)
/home/index.do does not match.
/index.do does match.
---------
feyd's pattern: ^/([^/]+/)*([^/]+\.do)$
/home/index.do does match.
/index.do does match.
Posted: Sun Jul 22, 2007 9:57 pm
by Pyrite
feyd, did you used to be a regular poster in the Unix forum on Quake3World ?
Posted: Sun Jul 22, 2007 10:18 pm
by feyd
Pyrite wrote:feyd, did you used to be a regular poster in the Unix forum on Quake3World ?
Nope.