Regular expressions are often difficult to understand and even more difficult to write. Probably one of the most confusing topics when you're starting is the escaping special characters.
Regular expressions (regex for short) are tools for matching patterns in text. There are some special characters with special meaning (their explanation is not the point of this post), and if we want to use those characters without its special meaning, we escape them by putting a backslash (
\) before them. Note that this makes
\ a special character too!
A simple example: the regex to match exactly
Count balance: 50$ will be
Count balance: 50\$.
What happens with is that in most languages
\ is a special character for strings! So the string to build the regex of the previous example would be:
Count balance: 50\\$.
Think about the translation from match to regex to string, not from string to regex to match.
What if we wanted to match
[ERR] $var has value "gg"?
[ERR] $var has value "gg"
"are special characters in this regular expression. We escape them:
\[ERR\] \$var has value \"gg\"
"`are special characters in this string. We escape them:
\\[ERR\\] \\$var has value \\\"gg\\\"
The thing is to write down an example of what you want to match and escape step by step the special characters for the regex and for the string until you have the one that you must use in your program.
(Links to Stack Overflow answers)
But beware! Be sure to use this kind of functions with regex in which you don't want special characters, because if you're not careful, you can end up escaping something you shouldn't and messing up the regex.