4

I have this string "u2x4m5x7" and I want replace all the characters but a number followed by an x with "". The output should be: "2x5x" Just the number followed by the x. But I am getting this: "2x45x7"

I'm doing this:

String string = "u2x4m5x7";

String s = string.replaceAll("[^0-9+x]","");

Please help!!!

3 Answers 3

3

Here is a one-liner using String#replaceAll with two replacements:

System.out.println(string.replaceAll("\\d+(?!x)", "").replaceAll("[^x\\d]", ""));

Here is another working solution. We can iterate the input string using a formal pattern matcher with the pattern \d+x. This is the whitelist approach, of trying to match the variable combinations we want to keep.

String input = "u2x4m5x7";
Pattern pattern = Pattern.compile("\\d+x");
Matcher m = pattern.matcher(input);
StringBuilder b = new StringBuilder();

while(m.find()) {
    b.append(m.group(0));
}

System.out.println(b)

This prints:

2x5x
Sign up to request clarification or add additional context in comments.

2 Comments

@HarshalParekh Yes, it can be done using String#replaceAll. But keep in mind that replaceAll uses a pattern matcher under the hood, so the second part of my answer is not as ugly as it might appear.
This one is the one :D, thank you so much!! You guys are awesome!.
2

It looks like this would be much simpler by searching to get the match rather than replacing all non matches, but here is a possible solution, though it may be missing a few cases:

\d(?!x)|[^0-9x]|(?<!\d)x

https://regex101.com/r/v6udph/1

Basically it will:

  • \d(?!x) -- remove any digit not followed by an x
  • [^0-9x] -- remove all non-x/digit characters
  • (?<!\d)x -- remove all x's not preceded by a digit

But then again, grabbing from \dx would be much simpler

Comments

2

Capture what you need to $1 OR any character and replace with captured $1 (empty if |. matched).

String s = string.replaceAll("(\\d+x)|.", "$1");

See this demo at regex101 or a Java demo at tio.run

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.