Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default different String handling as arg toregular expression class Pattern.compile

    Pattern.compile takes a String as argument, if a BufferedReader reads a line with readLine() ir returns a String, So a Regex like hello\sworld read from a file
    is passed in to Pattern.compile() it compiles fine and works fine in a Matcher to match text of "hello world" (without the quotes).

    But using the same code in a String literal (as below) gives a compile failure (yes some literature does say the String class swallows the escapes in cases so therefore the need for double back slashing.

    So p = Pattern.compile( "hello\sworld"); FAILS but b = br.readline() where readline has read hello\sworld WORKS.

    While p = Patterm.compile("hello\\sworld"); works fine.

    All look ups on String literal vs String object only talk about difference in memory pool vs heap but NOT any differences in parsing.

    Why the difference? Is there any way for Pattern.compile to take the String literal without the need to manually do the double back slahes?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: different String handling as arg toregular expression class Pattern.compile

    p = Pattern.compile( "hello\sworld");
    This isn't valid Java, and won't compile.

    Exactly what you can have between the " signs in a String literal is described in the JLS 3.10.5 String Literals. In particular only certain things are allowed as escape sequences (listed in 3.10.6) and \s isn't one of them.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: different String handling as arg toregular expression class Pattern.compile

    To expand a bit.
    '\' is the escape character for Java Strings.
    Since '\' marks certain commands in Regex, you need to escape the escape in order for it to be passed to regex and not treated as an escape sequence by the Java compiler. So '\\' gets translated by the compiler into a normal '\'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    sarokum is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default Re: different String handling as arg toregular expression class Pattern.compile

    thanks

Similar Threads

  1. Replies: 12
    Last Post: 10-20-2012, 06:55 PM
  2. Replies: 5
    Last Post: 08-16-2012, 07:02 AM
  3. Pattern.compile Help
    By Radu in forum New To Java
    Replies: 7
    Last Post: 04-14-2011, 02:31 PM
  4. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  5. Pattern expression for strings
    By chico9 in forum New To Java
    Replies: 8
    Last Post: 04-06-2010, 05:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •