[英]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 "".我有这个字符串“u2x4m5x7”,我想用“”替换所有字符,但一个数字后跟一个x。 The output should be: "2x5x" Just the number followed by the x.输出应该是:“2x5x” 只是数字后跟 x。 But I am getting this: "2x45x7"但我得到了这个:“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:这是一个使用String#replaceAll
和两个替换的单行:
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
.我们可以使用具有模式\\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).捕获您需要的$1
或任何字符并替换为捕获的$1
(如果|.
匹配$1
空)。
String s = string.replaceAll("(\\d+x)|.", "$1");
See this demo at regex101 or a Java demo at tio.run在 regex101 上查看此演示或在 tio.run 上查看Java 演示
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 https://regex101.com/r/v6udph/1
Basically it will:基本上它会:
\\d(?!x)
-- remove any digit not followed by an x
\\d(?!x)
-- 删除后面没有x
任何数字[^0-9x]
-- remove all non-x/digit characters [^0-9x]
-- 删除所有非 x/数字字符(?<!\\d)x
-- remove all x
's not preceded by a digit (?<!\\d)x
-- 删除所有x
前面没有数字的But then again, grabbing from \\dx
would be much simpler但话又说回来,从\\dx
抓取会简单得多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.