Results 1 to 9 of 9
Thread: Regular Expression Problem
- 02-10-2009, 03:13 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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
- 02-10-2009, 03:19 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Java Code:"(?<!;) (?!&)"
- 02-10-2009, 03:36 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
Thanks for your reply masijade. I tried the expression but it also replaces the first occurrence of 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; another;test
Also, the expression replaces   but leaves the semicolon (;) in the output (as in the above output).
- 02-10-2009, 03:53 PM #4
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
i get the correct output with that expression
test anothertest
with this code:
Java Code:String s = "test another test"; String s2 = s.replaceAll("(?<!;) (?!&)", ""); System.out.println(s2);
- 02-10-2009, 04:24 PM #5
You could also try
This does a negative look behind and a negative look ahead for " ", which seems to be what you are wanting to do.Java Code:String string = "test another test"; string = string.replaceAll("((?<! )( )(?! ))", "");
Looks like Wolfcro posted just before I did!Last edited by Steve11235; 02-10-2009 at 04:26 PM. Reason: add a note...
- 02-10-2009, 04:38 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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
- 02-10-2009, 04:46 PM #7
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 " " to make sure that the was no " " just ahead or just after.
- 02-10-2009, 05:16 PM #8
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
Thx a lot guys!
- 02-10-2009, 06:45 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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 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 will prevent from being removed. If you don't mind removing any additional whitespace around the then I would do it this way, to be sure
Java Code:"(?<! )\\s* \\s*(?! )"
Last edited by masijade; 02-10-2009 at 06:50 PM.
Similar Threads
-
Quantifiers in Regular Expression
By cdpm in forum java.utilReplies: 0Last Post: 12-24-2008, 01:03 PM -
Complex Regular Expression HELP
By hiklior in forum New To JavaReplies: 1Last Post: 04-30-2008, 01:52 PM -
regular expression for unicode
By tharhan in forum Advanced JavaReplies: 0Last Post: 04-01-2008, 10:53 PM -
Regular expression with Unions
By Java Tip in forum Java TipReplies: 0Last Post: 01-09-2008, 12:03 PM -
Regular expression with Intersections
By Java Tip in forum Java TipReplies: 0Last Post: 01-09-2008, 12:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks