Results 1 to 9 of 9
  1. #1
    daflores is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default Regular Expression Problem

    Hello all,

    A small example of the problem I have:

    String:
    test   another test

    In this string, I want to replace   (say by empty string) but ONLY when there is just a single occurrence. For example, this is the desired output for teh previous string:

    Desired Ouput:
    test   anothertest

    Is there a regular expression that could be used in the replace method that could accomplish this? I have tried with several expressions with no success but I am no expert in regular expressions.

    If this can not be done usign a regular expression, what would be the most efficient way of accomplishing this? Using the indexOf method?

    Thanks in advance for your help.

    David

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    "(?<!;)&nbsp;(?!&)"

  3. #3
    daflores is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks for your reply masijade. I tried the expression but it also replaces the first occurrence of &nbsp; even when there are several consecutive occurrences of it. For the string I gave as an example, the output using your regular expression is:

    test;&nbsp;&nbsp;another;test

    Also, the expression replaces &nbsp but leaves the semicolon (;) in the output (as in the above output).

  4. #4
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    i get the correct output with that expression

    test&nbsp;&nbsp;&nbsp;anothertest

    with this code:

    Java Code:
    String s = "test&nbsp;&nbsp;&nbsp;another&nbsp;test";
    		
    String s2 = s.replaceAll("(?<!;)&nbsp;(?!&)", "");
    System.out.println(s2);

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You could also try
    Java Code:
    String string = "test&nbsp;&nbsp;&nbsp;another&nbsp;test";
    string = string.replaceAll("((?<!&nbsp;)(&nbsp;)(?!&nbsp;))", "");
    This does a negative look behind and a negative look ahead for "&nbsp;", which seems to be what you are wanting to do.

    Looks like Wolfcro posted just before I did!
    Last edited by Steve11235; 02-10-2009 at 04:26 PM. Reason: add a note...

  6. #6
    daflores is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default Working now

    Thank you all for your replies. I initially tried masijade on a replace applet on the web and it did not work but when I saw wolfcro's reply I tried it again now directly in the code and it worked beautifully. Not sure why teh applet gave different results. Thanks for your reply too Steve, I'll give it a try too. Now if you could only shed some light on why it works I would appreciate it (does not need to be long). As I said I am no expert in regular expressions.

    Thanks,
    David

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Wolfcro and I did the same thing. He's been beating me to answering questions all morning.

    Here's a link to a reference site. Look at the Lookaround section.

    Basically, look after, placed after some other construct, tells the engine that, after it finds the construct, to look immediately after the construct to see if specified text is found. Look ahead works in a similar manner, and both have negative (not found) versions.

    In your case, we looked ahead and after "&nbsp;" to make sure that the was no "&nbsp;" just ahead or just after.

  8. #8
    daflores is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Thx a lot guys!

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by daflores View Post
    the expression replaces &nbsp but leaves the semicolon (;) in the output (as in the above output).
    Then you used it before I modified it. The first time I had forgotten the semicolon (there was only about a minute were you could have loaded the wrong one, but seemingly you managed to fit into that window). That would also be the reason for it having removed the first one. If you look at the expression again, you will see a subtle difference (the semicolon after nbsp). Try it that way.

    Edit: Also, that expression, as it is, will simply remove any &nbsp; without a ; directly before it and/or without a & directly behind it. A single whitespace between those html codes will allow the thing to be removed, and any html code before or after &nbsp; will prevent &nbsp; from being removed. If you don't mind removing any additional whitespace around the &nbsp; then I would do it this way, to be sure

    Java Code:
    "(?<!&nbsp;)\\s*&nbsp;\\s*(?!&nbsp;)"
    Last edited by masijade; 02-10-2009 at 06:50 PM.

Similar Threads

  1. Quantifiers in Regular Expression
    By cdpm in forum New To Java
    Replies: 0
    Last Post: 12-24-2008, 01:03 PM
  2. Complex Regular Expression HELP
    By hiklior in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 01:52 PM
  3. regular expression for unicode
    By tharhan in forum Advanced Java
    Replies: 0
    Last Post: 04-01-2008, 10:53 PM
  4. Regular expression with Unions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 12:03 PM
  5. Regular expression with Intersections
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 12:03 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
  •