简体   繁体   中英

Java regex, replace certain characters except

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!!!

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM