Re: Regular Expression reserved letters



On Apr 30, 7:18 am, adria...@xxxxxx wrote:
Hi all

I have the following problem.

I have to test if a text matches with another text. For this i use the
matches-Method of the String class in the way:

String a = "Hello world";
String b = "anytext goes here_-)_some m[]ore text goes{}here.";

if (a.tolowercase().matches(b)) {
// some code goes here

}

The problem ist, that e.g. ),[],{} are reserved words in regex. To use
them in filter expressions i have to escape them with \). But how to
know what all i have to escape? There are so many other reserved
letters and signs in regex and the text i have to compare can contain
everything.

So my questions:
Is there in any class any helper function which knows all the reserved
sings and letters and that supplies a function which escapes me all
automatically?

Thank you
Adrian

Well, there are two approaches to this...

One, is use a non-regex comparison. If you don't need regex, then
don't use regex. Regex is significantly slower than using indexOf or
equals.

If, on the other hand, you have a portion of an expresion that must be
regex, and a portion that must be litaral, I suggest you read the
documentation here: <http://java.sun.com/j2se/1.4.2/docs/api/java/util/
regex/Pattern.html>

That documentation will tell you what codes need to be escape, and
ironically, you can use replaceAll to appropriately escape the string.

Hope this helps,
Daniel.

.



Relevant Pages

  • Re: Character Escapes Dont Work in VB Regex Replace?
    ... VB.NET just does not support C# escape sequences, ... not the replace string according to the Character Escapes section of Regular ... as the RegEx class itself is defining this behavior. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Character Escapes Dont Work in VB Regex Replace?
    ... >> does work as it says in the documentation, ... They do not work in that RegEx will not honor them, ... Where I am telling C# not to replace C#'s escape sequences. ... supported both in regular expressions and replacement patterns. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Character Escapes Dont Work in VB Regex Replace?
    ... RegEx.Unescape that will escape & unescape strings for you (including ... supported both in regular expressions and replacement patterns. ... wanted to see if this was a VB specific issue, or the .net regex engine. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Control characters - regex to match/lose these?
    ... The is causing the RE to match 'escape, ... by (either a multi-char escape sequence or one of the single-char ... understand, that regex. ... any number of times (though not necessarily the same char) but only if ...
    (comp.lang.perl.misc)
  • Re: how to split a string using ,fixed character length, variable text delimmiter
    ... Thankyou developer x, i'm not getting the desired result. ... characters. ... recognise the escape sequence, ... Regex r = new Regex(@"[ ...
    (microsoft.public.dotnet.languages.csharp)