Java and regular expressions...
Results 1 to 3 of 3

Thread: Java and regular expressions...

  1. #1
    Senior Member
    Join Date
    Dec 2004
    Posts
    107

    Java and regular expressions...

    Hi Everyone,

    It's been a while since I've been here. Glad to be back though!

    I have a quick question for someone knowledgable in Java (using 1.4.2_07):

    I need to escape the "<" and ">" characters in a regex (more specifically, String.replaceAll() method), and I've tried everything from hex codes for the ascii values to escaping them with "\\>" and "\\<", but I still get an exception. I'm dyin here! Any ideas anyone??

    Thanks
    -ik
    Alright Brain, you don\'t like me, and I don\'t like you. But let\'s just do this, and I can get back to killing you with beer.
    -- Homer S.

  2. #2
    Junior Member
    Join Date
    Aug 2003
    Posts
    4
    Hi there,

    You don't need to escape "<" or ">" characters!


    For example, this works perfectly to strip html tags from a string:

    String test = "<html> test string </html>";
    test = test.replaceAll("<.*>(.*)<.*>","$1");
    test = test.trim();

    test will contain: "test string".



    Anyways incase its not something that obvious, I use java a lot so perhaps I can help... but first can you post the exception you are getting ?


    Inferno.

  3. #3
    Senior Member
    Join Date
    Dec 2004
    Posts
    107

    Problem solved

    Hey, thanks for the reply.

    I solved the problem earlier by making a generic function that will escape any given string's special characters. Actually, I borrowed that function from javaalmanac.com (search for regular expression, the method is called escapeRE).

    Anyway, turns out the problem wasn't in my regular expression string (that is, replaceAll(String regex, String replacement), in regex), but in my replacement string -- it needs to be escaped too (duh!). It only took me a few hours to figure that out hehe

    Anyway, thanks again for the help. We should keep in touch as fellow Java programmers.

    Have a nice day!

    -ik
    Alright Brain, you don\'t like me, and I don\'t like you. But let\'s just do this, and I can get back to killing you with beer.
    -- Homer S.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •