Solved

help with regular expressions

Posted on 2006-07-20
11
247 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__
ID: 17145855
if (!test.contains(" +"))
   test.removeAll(" ");
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17145868
search for \+\s+
replace with +

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

Expert Comment

by:colr__
ID: 17145873
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:Mayank S
ID: 17145890
And since String is immutable:

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

Expert Comment

by:CEHJ
ID: 17145945
test.replaceAll("\\+\\s*", "\\+");
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
ID: 17145948
Of course that would be

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

Expert Comment

by:CEHJ
ID: 17145960
(without the final bracket ;-))
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 20 total points
ID: 17146021
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
ID: 17146073
>>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
ID: 17213681
Thanks a lot for the help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17213690
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
json example 39 135
create a gui in perl 3 72
@SBGen Method 3 38
java.io.BufferedReader   readLine()  //returns the entire file ? 4 60
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

867 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

23 Experts available now in Live!

Get 1:1 Help Now