Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Regular Expression reserved letters

Thread view: 
adrianae@gmx.ch - 30 Apr 2007 15:18 GMT
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
Chris Dollin - 30 Apr 2007 15:35 GMT
> 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.

Signature

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

Daniel Pitts - 30 Apr 2007 15:35 GMT
On Apr 30, 7:18 am, adria...@gmx.ch wrote:
> Hi all
>
[quoted text clipped - 24 lines]
> 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.
Daniel Pitts - 30 Apr 2007 15:47 GMT
> On Apr 30, 7:18 am, adria...@gmx.ch wrote:
> > Hi all
[quoted text clipped - 11 lines]
>
> > }
Oops, I should have suggested using:
if (a.equalsIgnoreCase(b)) {
}

Hope this helps too,
Danie.
adrianae@gmx.ch - 01 May 2007 15:22 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.