• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Pattern question

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking at some code that extracts a username from some text of the form "@name - this is a message". The code is:



When run, the above prints out: "Found: name". I understand most of what is going on, except for the '\\@' portion of the regex. I'm not sure why there are two backslashes here. This will yield the same result if I remove those two backslashes.
 
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because backslash in a String literal is special. It introduces special character sequences that represent other characters, most well known being \n that represents newline. Since backslash is special, it cannot stand alone; if you try it, the compiler will assume that the immediately following character is to be interpreted as special. So, if you want a backslash in a String or a char literal, you must use two, which creates a "special character sequence" that represents the single backslash.
 
Mike Tabak
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Roberts wrote:Because backslash in a String literal is special. It introduces special character sequences that represent other characters, most well known being \n that represents newline. Since backslash is special, it cannot stand alone; if you try it, the compiler will assume that the immediately following character is to be interpreted as special. So, if you want a backslash in a String or a char literal, you must use two, which creates a "special character sequence" that represents the single backslash.



That seems to imply what the pattern is looking for is something of the form "\@name", but this isn't the case. Note the test string I am using, it contains "@name...." not "\@name...".
 
Simon Roberts
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Backslash is also special to regular expressions, in the same way as they are to String literals. However, I have no clue what most of the regex special character sequences are, so I can't comment further. Look up java.regex.Pattern in the API docs, and you'll find screeds of info on those. So \@ presumably means something, though I don't know what. For 100% sure, however, \\ in a string literal creates a single backslash in the resulting string.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two backslashes in front of the @ are not necessary at all in this case. The person who wrote this code probably thought "hmmm, @ is probably a special character in a regular expression, I need to escape it" and didn't bother to look it up. But @ is not a special character in a regex. You can remove the backslashes and it works exactly the same.

(In fact, my IDE, which is IntelliJ, already warns me about this, it says "Redundant character escape" when I put backslashes in front of the @).
 
Simon Roberts
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, indeed, I had previously read the first half of this (which is from the API docs page):

It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct.



... which made me think that this must be something special I didn't know about, but the second half, while surprising, seems to clearly indicate that in this case the redundancy is permitted.

I apologize to OP, I thought you were asking why there were two, as opposed to one, backslashes. Now I realize that actually, you were asking why there were any at all
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic