Regular Expressions Escaping Strings


In most programming languages, in order to have a backslash in a string generated from a string literal, each backslash must be doubled in the string literal. Otherwise, it will be interpreted as an escape for the next character.

Unfortunately, any backslash required by the regex must be a literal backslash. This is why it becomes necessary to have "escaped escapes" (\\) when regexes are generated from string literals.

In addition, quotes (" or ') in the string literal may need to be escaped, depending on which surround the string literal. In some languages, it is possible to use either style of quotes for a string (choose the most readable one for escaping the entire string literal).

In some languages (e.g.: Java <=7), regexes cannot be expressed directly as literals such as /\w/; they must be generated from strings, and normally string literals are used - in this case, "\\w". In these cases, literal characters such as quotes, backslashes, etc. need to be escaped. The easiest way to accomplish this may be by using a tool (like RegexPlanet). This specific tool is designed for Java, but it will work for any language with a similar string syntax.