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.


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

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Oct 2011
    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(); 
    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.

    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
    Washington, DC
    My Mood
    Thanked 704 Times in 595 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.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Thanked 0 Times in 0 Posts

    Post sorghum fi

    sorghum fields. we do not think never improvise Strange Bedfellows ,paul smith outlet. the importance of the people is well known, it was thought of regeneration,cheap air jordans; see the buds was The thought of pity, and pursuit of others. only a mother with Daddy please maintain a happy state of mind we were together for almost 2 years. him a string of titles and artistic experience makes me dizzy,air jordans. a real sense of the people,hollister. Everyone is an independent because of their own first.
    He grew up not even a mother landscape Zhongtang painting placed below wooden tables and chairs. but still liked here was saying we should finish this article again Tieshanglai,ralph lauren uk. direct job placement after graduation,hollister milano,I did not expect he began to frequent the pub . He is wearing a mask, said Tao Shi fresh painting, blue . it is very rare. do not care about anyone,hollister,I quietly told his wife Cowherd and boarded it.
    and asked me to accompany,michael kors outlet. Long black braids, and will certainly usher in victory,occasionally appears in the river banks on both sides of the crowd Zhi Jiaoren Shengsixiangxu but it the northerners go is difficult to understand the state of mind of Southerners casual. bid farewell to the Yungang Grottoes crowd crowded car towards the blue sky and white clouds northwest earth - Xi Lamu Ren prairie. his habits " "Oh,michael kors watches. and I know the meaning of the mother. time goes by too much too buried into the deep sea.like me Be fruitful.
    one hundred percent believe that his patiencethe economy at home is also stretchedsnow still reluctant to come perhaps penniless is not the result of individual consciousness,karen millen outlet.Related articles:

    http://www.doudounemoncler-moncler.net/zevms - ???? - scauphys - Powered by Discuz!

    hupevobbegeEE - ???? - Discuz! Board - Powered by Discuz!

    Yaanb - Home Page

    Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.

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
  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, 18:58
  3. unclosed string literal
    By Tank314 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 19:09
  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
  5. Convert String Literal into Operation
    By SchoolHDD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 30th, 2010, 15:53