Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem specifying \ in a string literal Using Matcher/Patteren class's

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem specifying \ in a string literal Using Matcher/Patteren class's

    Hi , I have created a simple program below to test how java handles regular expressions , I have created a pattern object p specifying an array of characters as the parameters, then created the Matcher object m using p as the pattern and a string of characters as the parameter then invoked the matches()on the matcher object m and printed the boolean result to the console.
    import java.util.regex.Pattern;
    import java.util.regex.Matcher; 
    public class regexTest {
    public static void main (String []args) {
    Pattern p = Pattern.compile("[a-zA-Z0-9-
    /]+"); 
    Matcher m = p.matcher("123456-07//");
    boolean b = m.matches();
    String s1 = m.toString(); 
    System.out.println(m.matches());
    System.out.print(m.toString());
    }
    }
    The program works fine and returns a Boolean true to the console indicating the match was successful for the following
    Pattern p = Pattern.compile("[a-zA-Z0-9-
    /]+");
    Matcher m = p.matcher("123456-07//");

    Result = true
    java.util.regex.Matcher[pattern=[a-zA-Z0-9-\/]+ region=0,11 lastmatch=123456-07//]<<< Process finished.
    The problem occurs when I want to add the \ character to the pattern and matcher class character sequence.

    Note: As backslashes within string literals in java are interpreted as character escapes its necessary to use double backslash
    to represent a single backslash, thus the expression
    matches a single backslash.

    Adding
    to both Pattern and Matcher :

    Pattern p = Pattern.compile("[a-zA-Z0-9-
    /]+");
    Matcher m = p.matcher("123456-07
    //");

    Result = false
    java.util.regex.Matcher[pattern=[a-zA-Z0-9-\/]+ region=0,12 lastmatch=]<<< Process finished.
    As Can be seen the Result retuned is false and there is no match

    How can I specify the \ character in the pattern and matcher in order to have a Boolean True returned??
    I have tried using a single character \ in the Matcher but the compiler does not like it.
    egexTest.java:9: illegal escape character
    Matcher m = p.matcher("123456-07\//"

    Any Help or direction on this simple but highly frustrating problem greatly appreciated.

    Thanks in advance


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,438
    My Mood
    Hungover
    Thanks
    144
    Thanked 634 Times in 542 Posts

    Default Re: Problem specifying \ in a string literal Using Matcher/Patteren class's

    I think you're confusing backward and forward slashes. Can you throw together another example with only the String and pattern you're trying to match against? It might seem obvious to you, but I'm not sure which of those lines you're attempting to use.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] java.util.regex.Matcher value lengths
    By PDubUK in forum Java SE APIs
    Replies: 1
    Last Post: August 15th, 2011, 08:14 AM
  2. [SOLVED] How to compare a parameter value to a string literal inside EL?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 28th, 2011, 06:58 PM
  3. unclosed string literal
    By Tank314 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 07:09 PM
  4. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 08:58 AM
  5. Convert String Literal into Operation
    By SchoolHDD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 30th, 2010, 03:53 PM