Results 1 to 8 of 8
  1. #1
    nikoulini85 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Add an exception in a replaceAll method

    Hello everyone!

    I have added the code below in my project for security reasons, but I would like to add an exception only for links to websites.
    So an exception for "<a" and "</a>".
    Can you please help me since I cannot find a solution to this?

    message = message.replaceAll("<", "&lt;");
    message = message.replaceAll(">", "&gt;");

    Kind regards

  2. #2
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Add an exception in a replaceAll method

    You could use dojo.string.contains() method.

    Take a look here
    More fun with strings: dojo.string.contains() | Jens Arps

  3. #3
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Add an exception in a replaceAll method

    I'm not sure how alexandras tip would work for you, it seems to be a 3rd party API, I'm not even sure its for Java. (?)

    I guess that you know what a Java Exception is, and I've understood that this is not what you are asking about. Have I understood you correctly if you want to replace characters in a string, with reservation for certain strings that contain a certain pattern of characters?

    You can use an if-statement and check if it has the sequence of characters.

    Java Code:
    if(message.contains("<a") || message.contains("</a>")){
    	//do this
    }else{
    	//do something else
    };
    Cheers,
    Z!

  4. #4
    nikoulini85 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Add an exception in a replaceAll method

    Thank you both...

    Zyril's suggestion is easier for me to understand so I will stick to it.

    Best,

    N

  5. #5
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Add an exception in a replaceAll method

    Quote Originally Posted by nikoulini85 View Post
    Thank you both...

    Zyril's suggestion is easier for me to understand so I will stick to it.

    Best,

    N
    Since you are using replaceAll() I assume you want to replace all on a line except the <a and the </a but Zyril suggestion will not replace those on a line that need replacing when, as is possible, there are some that need to be replaced and some that do not.

    It seems to me you need to use 'negative look ahead' (see the Pattern class for details) to exclude the anchor tags.

  6. #6
    nikoulini85 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Add an exception in a replaceAll method

    I did this and I think that it works ok for the moment. I am a newbie as you can see:


    class Main {
    public static void main (String[] args) {

    String topMessage = "<a href=http://www.google.com>Google</a>";

    if ((!topMessage.contains("<a href")) || (!topMessage.contains("</a>"))){

    topMessage = topMessage.replaceAll("<", "&lt;");
    topMessage = topMessage.replaceAll(">", "&gt;");
    };

    System.out.println(topMessage);

    }
    }

  7. #7
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Add an exception in a replaceAll method

    Quote Originally Posted by nikoulini85 View Post
    I did this and I think that it works ok for the moment. I am a newbie as you can see:


    class Main {
    public static void main (String[] args) {

    String topMessage = "<a href=http://www.google.com>Google</a>";

    if ((!topMessage.contains("<a href")) || (!topMessage.contains("</a>"))){

    topMessage = topMessage.replaceAll("<", "&lt;");
    topMessage = topMessage.replaceAll(">", "&gt;");
    };

    System.out.println(topMessage);

    }
    }
    Suppose a line contains :-

    Hello <a href="www.bbc.co.uk">BBC</a> cruel <p>world</p>

    I think your code will fail to deal with the <p> and the </p> since the "<a href=" will stop the line being processed by the replaceAll() pair.

    And what will happen if there are multiple white spaces between the <a and the href?
    Last edited by sabre150; 08-23-2012 at 07:33 PM.

  8. #8
    nikoulini85 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Add an exception in a replaceAll method

    Quote Originally Posted by sabre150 View Post
    Suppose a line contains :-

    Hello <a href="www.bbc.co.uk">BBC</a> cruel <p>world</p>

    I think your code will fail to deal with the <p> and the </p> since the "<a href=" will stop the line being processed by the replaceAll() pair.

    And what will happen if there are multiple white spaces between the <a and the href?

    Sambre150 thanks for your comments.

    I ended up making this regex: message=message.replaceAll("(<)((?:(?!a href|/a).)*?)(>)","&lt;$2&gt;");

    It works fine in general except when we put something like <<text>. It has an issue only with the less than sign.

    I guess it crashes with the HTML code of the page.

    Would you have any idea why?

Similar Threads

  1. The meaning of replaceAll
    By herolua in forum New To Java
    Replies: 4
    Last Post: 10-07-2011, 06:18 AM
  2. Problem with replaceAll
    By Timmy in forum New To Java
    Replies: 6
    Last Post: 06-06-2011, 05:51 PM
  3. replaceAll Problem
    By steve_m in forum New To Java
    Replies: 3
    Last Post: 12-22-2010, 02:09 PM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 06:48 AM
  5. Replies: 1
    Last Post: 03-08-2008, 07:41 PM

Posting Permissions

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