Regular Expression reserved letters

Discussion in 'Java' started by adrianae@gmx.ch, Apr 30, 2007.

  1. Guest

    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
    , Apr 30, 2007
    #1
    1. Advertising

  2. Chris Dollin Guest

    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
    Chris Dollin, Apr 30, 2007
    #2
    1. Advertising

  3. Daniel Pitts Guest

    On Apr 30, 7:18 am, 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.
    Daniel Pitts, Apr 30, 2007
    #3
  4. Daniel Pitts Guest

    On Apr 30, 7:35 am, Daniel Pitts <> wrote:
    > On Apr 30, 7:18 am, 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.
    Daniel Pitts, Apr 30, 2007
    #4
  5. Guest

    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
    , May 1, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metaperl
    Replies:
    32
    Views:
    906
    NickC
    Sep 15, 2006
  2. Delaney, Timothy (Tim)
    Replies:
    10
    Views:
    667
    Jason
    Sep 14, 2006
  3. Steve Holden
    Replies:
    0
    Views:
    394
    Steve Holden
    Sep 13, 2006
  4. Merrigan
    Replies:
    4
    Views:
    565
    Chris
    Dec 14, 2007
  5. Replies:
    3
    Views:
    163
    osmium
    Nov 6, 2013
Loading...

Share This Page