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 3 of 3

Thread: Question to Pattern.matches and escape sequences

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question to Pattern.matches and escape sequences

    Hello,
    I have a question regarding regular expressions and the Pattern.matches function. Right now I'm experimenting around a bit and found a strange behaviour I can't explain. Maybe some of you can.
    Let's say we have:
    String s= "abc\nabc";
    System.out.println(Pattern.matches("abc\nabc", s));
    System.out.println(Pattern.matches("abc\\nabc", s));
    System.out.println(Pattern.matches("abc\\\nabc", s));
    Why do all three matches yield true?
    For the second one it's clear, since \n is a regex-expression and the first backslash quotes it. But the rest is not so clear, especially the third one. Can someone explain it to me?


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Question to Pattern.matches and escape sequences

    The first one matches because the two strings are exactly the same. The "\n" in both strings represents a new line.

    In the second one, "\\" is read as a single "\" in an input string. This may match because the "n" following is then treated literally and NOT as a new line.

    The third one is weird... The first "\\" would be read as a "\" in the input string, so I don't know how there's something to match the new line...

    "abc\\\nabc" represents this string:

    "abc\
    abc"

    The input string could be either

    "abc
    abc" (as the first matcher finds)

    or

    "abc\nabc" (as the second matcher finds)

    So I honestly don't know what's going on with that third one. To learn more about regular expressions, check out Oracle's tutorial. It's quite enlightening.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question to Pattern.matches and escape sequences

    Ok, thank you. I thought that this is something obvious and I'm just to blind to see it

Similar Threads

  1. Generate all possible sequences from Event Tree
    By masterblaster in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 22nd, 2011, 02:57 AM
  2. Using \n escape sequence in a toString method
    By coolidge in forum Java Theory & Questions
    Replies: 4
    Last Post: September 22nd, 2011, 04:14 PM
  3. [SOLVED] Pattern, regex (regular expression) case sensitivity question
    By chronoz13 in forum Java SE APIs
    Replies: 1
    Last Post: September 7th, 2011, 04:16 AM
  4. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  5. Replies: 2
    Last Post: March 4th, 2009, 06:32 AM