We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Java Strings use the backslash as an escape character, as do regular expressions.
This is why regular expressions look so messy when using shorthands for word characters (\w) etc. We mean "\w" but we need to write "\\w" in Java as the first backslash is the escape character, and the second backslash tells it we actually mean a backslash.
Internally, only a single backslash is used so "\\w" written in your code actually is stored as "\w" and is used as such in regular expressions.
I hope this helps - although it may disappoint!
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Regex 2 - Duplicate Words
You are viewing a single comment's thread. Return to all comments →
No, unfortunately.
Java Strings use the backslash as an escape character, as do regular expressions.
This is why regular expressions look so messy when using shorthands for word characters (\w) etc. We mean "\w" but we need to write "\\w" in Java as the first backslash is the escape character, and the second backslash tells it we actually mean a backslash.
Internally, only a single backslash is used so "\\w" written in your code actually is stored as "\w" and is used as such in regular expressions.
I hope this helps - although it may disappoint!