Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regular Expression reserved letters

Reply
Thread Tools

Regular Expression reserved letters

 
 
adrianae@gmx.ch
Guest
Posts: n/a
 
      04-30-2007
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

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      04-30-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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?


Read the regexp documentation.

--
Jena user conference, September 2007: http://hpl.hp.com/conferences/juc2007/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      04-30-2007
On Apr 30, 7:18 am, (E-Mail Removed) 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.

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      04-30-2007
On Apr 30, 7:35 am, Daniel Pitts <(E-Mail Removed)> wrote:
> On Apr 30, 7:18 am, (E-Mail Removed) 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

>
> > }

Oops, I should have suggested using:
if (a.equalsIgnoreCase(b)) {
}

Hope this helps too,
Danie.

 
Reply With Quote
 
adrianae@gmx.ch
Guest
Posts: n/a
 
      05-01-2007
Hi Daniel

Thank you for your answer.

In my case the best solution is to supply one more parameter
indicating if i want to compare by regex or with equals().

Adrian

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression with accent letters aldobelli@gmail.com ASP General 5 03-05-2009 08:43 AM
Are Python's reserved words reserved in places they dont need to be? metaperl Python 32 09-15-2006 02:02 PM
RE: Are Python's reserved words reserved in places they dont needtobe? Delaney, Timothy (Tim) Python 10 09-14-2006 04:17 PM
Re: Are Python's reserved words reserved in places they dont needtobe? Steve Holden Python 0 09-13-2006 08:44 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments