Solved

help with regular expressions

Posted on 2006-07-20
11
246 Views
Last Modified: 2007-12-19
Hi,

I require some help with the regular exp in java.

I have a String say
String test="regular +expr + java+ example";

I will get the String as  req parameter.
If I find space before and after " + " I will have to remove that and replace that to test="regular +expr+java+example";(without any spaces)
if there is space after "+ " then the String should become test="regular +expr+java+example";(without any spaces)
If I have space only before " +"it should stay as it is test="regular +expr+java+example";

The only condition when the "+" will remain unaltered is when it is preceded by space(s) and immediately followed by text with no space in between.i.e(regular +expr).

Please help me.

Thanks.

0
Comment
Question by:ZOOMAY
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 8

Expert Comment

by:colr__
Comment Utility
if (!test.contains(" +"))
   test.removeAll(" ");
0
 
LVL 12

Expert Comment

by:enachemc
Comment Utility
search for \+\s+
replace with +

You might be required to escape the "\" character in Java.
0
 
LVL 8

Expert Comment

by:colr__
Comment Utility
In fact it might be this:

if (!test.contains(" +") && !test.contains("+ "))
   test.removeAll(" ");

Is this what you are looking for?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
And since String is immutable:

String newTest = test.removeAll ( " " ) ;
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
test.replaceAll("\\+\\s*", "\\+");
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
Comment Utility
Of course that would be

test = test.replaceAll("\\+\\s*", "\\+"));
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
(without the final bracket ;-))
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 20 total points
Comment Utility
CEHJ,
it should be
test.replaceAll("\\+\\s+", "\\+");

because if you use * instead of +, there might be a lot of useless replaces (like replace "+" with "+")
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>there might be a lot of useless replaces

I doubt it, as that would be fairly incompetent of 'them' to try to replace a token with an identical one, but you're right - it'd be better to err on the safe side
0
 

Author Comment

by:ZOOMAY
Comment Utility
Thanks a lot for the help.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now