IBM Support

Invalid escape sequence in a regular expression

Troubleshooting


Problem

A string contains a literal character that is a reserved character in the Regex class (for example, the '(' or open parentheses character). Placing a '\' (backslash) in front of the character in the regular expression generates an 'Invalid escape sequence' compilation error. This only occurs when the regular expression is used in the text of the script. The backslash character can be used as an escape sequence in a regular expression in a recognition property or a verification point.

Resolving The Problem

Instead of using the backslash as an escape sequence, specify the literal character by placing it in square brackets, for example, use

    Regex r = new Regex("[(]HelloWorld[)]");

instead of

    Regex r = new Regex("\(HelloWorld\)");

which generates the Invalid escape sequence error.

To re-iterate, the backslash character can be used as an escape sequence for a regular expression in a recognition property or a verification point. The compilation error only occurs when creating a regular expression in the script text.



[{"Product":{"code":"SSSHZT","label":"Rational XDE Tester"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"--","Platform":[{"code":"PF033","label":"Windows"}],"Version":"2003;2003.06.00;2003.06.01;2003.06.12;2003.06.13","Edition":"","Line of Business":{"code":"","label":""}}]

Document Information

Modified date:
16 June 2018

UID

swg21194641