Pattern in Java not working

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Pattern in Java not working

Post 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$
Last edited by Chris Corbyn on Tue Jul 10, 2007 5:13 pm, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

should that be or '/|\\', or have you double escaped '.' by mistake?

Try (if the former):

Code: Select all

^(?:/([^/]+))+/|\\\.do$
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jenk wrote:should that be or '/|\\', or have you double escaped '.' by mistake?

Try (if the former):

Code: Select all

^(?:/([^/]+))+/|\\\.do$
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 ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :cry:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Last edited by feyd on Tue Jul 10, 2007 5:31 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Looks like you misread my pattern ;)

^/([^/]+)(/|\.do$)

I'm an idiot too :oops: matches() tests the entire string. I need lookingAt().

Thanks for the help anyway :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

feyd, did you used to be a regular poster in the Unix forum on Quake3World ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Pyrite wrote:feyd, did you used to be a regular poster in the Unix forum on Quake3World ?
Nope.
Post Reply