Whenever I try to declare a "Regular Expression" while including symbols like "+", "-", "*", the match is done based on how those operators work.

Now, when I try to use the regex API via combining those operators with backslash or \Q and \E I get the error message--

"...Illegal escape character".

I'm using TextPad at the moment, however the error is thrown by the compiler which makes me wonder if I need to upgrade or if there's a specific file to download to use regex API legally.

"\\+"

instead of just

"+"

and likewise for the rest.

Hi Alex,
I see the problem you are facing, and have been there..
In Java when you are typing in a regex you need to understand that a simple "\" stands for indicating escape characters inside your string (Hence the error when you mention "\+" as there no such escape character in Java).
But you need to indicate an escape character in your regex so you need to mention it as "\\+", the first back slash tells the compiler to treat the next backslash as a normal backslash which in tells your regex evaluator that the next "+" is just a normal character and not a quantifier.

So in case you wish to search for a "\" in your regex you would have to type in "\\\\"

The back slashes in red are used by the compiler as an indication for an escape sequence
The back slash in green is used by your regex evaluator as the start of an escape sequence.
The final back slash is what is taken by the regex evaluator as a normal character.

Similarly if you wanted your regex to search for a "+" it would become : "\\+"

Thanks a million, both of you =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.