Java regex needed

Hi,

I would like to have a regex that

1. can take "+  700" out of  "+  700 bmw".
2. but won't take  "+  700" out of "+  700 bmw".

In the first case, there are one or more spaces between number 700 and bmw. There are none between number and string.
wsyyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
should be like this:
replaceAll("[\\d\\+\\s]+","");

Open in new window

0
for_yanCommented:
                 String ssss =   ("+  700 bmw").replaceAll("[\\d\\+\\s]+","");

         System.out.println("result: " + ssss);

Open in new window


Output:

result: bmw

Open in new window

0
chaitu chaituCommented:
("+  700 bmw").replaceAll(".*(?:\\d+\\s+)(\\w+)","$1");
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

wsyyAuthor Commented:
sorry none of the solution works.

I have two purposes:

first, for "+ 700 bwm", I want to get "bwm"

second, for "+ 700bwm", I want to get "+ 700bwm"
0
chaitu chaituCommented:
In first case any of the solutions will work.

In second case input and output seems to be same.
0
wsyyAuthor Commented:
chaituu,

you are right. I want to have one regex solution for the two cases.
0
wsyyAuthor Commented:
to be more specific, I want to have one REGEX

1.

String ssss =   ("+  700 bmw").replaceAll(REGEX,"");
ssss="bmw"

2.

String ssss =   ("+  700bmw").replaceAll(REGEX,"");
ssss="+  700bmw"
0
chaitu chaituCommented:
why do u need Regex for 2nd case.
0
wsyyAuthor Commented:
I have a function that use the regex to evaluate the string and take action accordingly.

the regex is the input.
0
for_yanCommented:
maybe this could work:
                 String ssss =   ("+  700 bmw").replaceAll("([\\d\\+\\s]+ )|([\\+\\s]+)","");

         System.out.println("result: " + ssss);

Open in new window

0
for_yanCommented:
sorry that was wrong
0
for_yanCommented:
But is "bmw" always "bmw" ?
0
for_yanCommented:
Then you can jkust replaceAll  " bmw" with "bmw"
0
for_yanCommented:
No ok, this needs to be done with computer - in thois ay I can only make mistakes
0
for_yanCommented:
Still you need to explain where is the variability, otherwise you take the first string as it is as a whole and change to bwm, and the second string in such case will not change - so your requirement will be met
0
wsyyAuthor Commented:
guys, appreciated. but the solutions above do not work. maybe is because i didn't make it clear enough.

let me put this way. I want to extract Numbers following either "+" or "-" from a string, and leave the rest intact.

When I mean numbers, I mean pure numbers, such as 12345, 0.12, 1,000,000, 8,001.21.

I don't want to take any embedded numbers out, such as "abc2", "700bmw", and "dafee23kjda".

Here are the examples:

input                         output
abc2                         empty
+  700bmw              empty
+  700 bmw             +   700
233abc                     empty
0
chaitu chaituCommented:
 Pattern p = Pattern.compile("(\\+\\s{2,}\\d+\\s).*?\\b");
                 Matcher m = p.matcher("Testing123Testing");
                  m = p.matcher("+  700bmw");
                 if (m.find()) {  
                       System.out.println(m.group(1));
                       }
0
chaitu chaituCommented:
Pattern p = Pattern.compile("(\\+\\s{2,}\\d+\\s).*?\\b");
              Matcher     m = p.matcher("+  700bmw");
                 if (m.find()) {  
                       System.out.println(m.group(1));
                       }
0
chaitu chaituCommented:
 Pattern p = Pattern.compile("(\\+\\s{2,}\\d+\\s)");
                  Matcher  m = p.matcher("+  700 bmw");
                 if (m.find()) {  
                       System.out.println(m.group(1));
                       }
0
CEHJCommented:
Try splitting the input first. Something like
String[] atoms = input.split("\\s");
	for(String s : atoms) {
	    if (s.matches("[0-9,.]+")) { // (tighten as necessary)
		System.out.printf("Found '%s'\n", s);
	    }
	}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
You'd need to check for + and - too
0
käµfm³d 👽Commented:
Try the following:

String input = "+  700bmw";
Pattern p = Pattern.compile("(?:[+-]\\s*)?\\b\\d+\\b");
Matcher m = p.Matcher(input);

while (m.find())
{
    System.out.println(m.group());
}

Open in new window

0
CEHJCommented:
e.g.
String[] atoms = input.split("(?<![+\\-])\\s");
	System.out.println(Arrays.toString(atoms));
	for(String s : atoms) {
	    if (s.matches("[ +\\-0-9,.]+")) {
		System.out.printf("Found '%s'\n", s);
	    }
	}

Open in new window

0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.