• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pattern matching  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is related to regular expression. In this example below, we are using the regular expression as \\d. I know \d stands for a digit. But what does \\d stand for?


 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's an explanation of this in the Java Pattern documentation. Read the section "Backslashes, escapes, and quoting".
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Now I have a string as "1\2\3" and I want to split on the \. How will the regular expression look like?
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's where it gets fun.

The Regex pattern for matching a backslash, is a backslash escaped backslash "\\"

However, to represent that in a Java String will require each of those backslashes to be escaped with a backslash. Thus a total of 4 backslashes:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!