We help IT Professionals succeed at work.

Java regex needed

wsyy
wsyy asked
on
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.
Comment
Watch Question

Awarded 2011
Awarded 2011

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

Open in new window

Awarded 2011
Awarded 2011

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

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

Open in new window


Output:

result: bmw

Open in new window

("+  700 bmw").replaceAll(".*(?:\\d+\\s+)(\\w+)","$1");

Author

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"
In first case any of the solutions will work.

In second case input and output seems to be same.

Author

Commented:
chaituu,

you are right. I want to have one regex solution for the two cases.

Author

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"
why do u need Regex for 2nd case.

Author

Commented:
I have a function that use the regex to evaluate the string and take action accordingly.

the regex is the input.
Awarded 2011
Awarded 2011

Commented:
maybe this could work:
                 String ssss =   ("+  700 bmw").replaceAll("([\\d\\+\\s]+ )|([\\+\\s]+)","");

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

Open in new window

Awarded 2011
Awarded 2011

Commented:
sorry that was wrong
Awarded 2011
Awarded 2011

Commented:
But is "bmw" always "bmw" ?
Awarded 2011
Awarded 2011

Commented:
Then you can jkust replaceAll  " bmw" with "bmw"
Awarded 2011
Awarded 2011

Commented:
No ok, this needs to be done with computer - in thois ay I can only make mistakes
Awarded 2011
Awarded 2011

Commented:
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

Author

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
 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));
                       }
Pattern p = Pattern.compile("(\\+\\s{2,}\\d+\\s).*?\\b");
              Matcher     m = p.matcher("+  700bmw");
                 if (m.find()) {  
                       System.out.println(m.group(1));
                       }
 Pattern p = Pattern.compile("(\\+\\s{2,}\\d+\\s)");
                  Matcher  m = p.matcher("+  700 bmw");
                 if (m.find()) {  
                       System.out.println(m.group(1));
                       }
Top Expert 2016
Commented:
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

Top Expert 2016

Commented:
You'd need to check for + and - too
Most Valuable Expert 2011
Top Expert 2015

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

Top Expert 2016
Commented:
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

Top Expert 2016

Commented:
:)