java Regex:用一个数字替换所有数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7737946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java Regex: replace all numerical values with one number
提问by user961627
I have a simple line of text which might include numbers like "12.3" or "1983" or "5/8". Whenever any number appears, I just need to replace with a fixed character, say the digit "8".
我有一行简单的文本,其中可能包含诸如“12.3”或“1983”或“5/8”之类的数字。每当出现任何数字时,我只需要用固定字符替换,比如数字“8”。
I've been fiddling about with Regex in Java, with things like this:
我一直在使用 Java 中的正则表达式,比如这样:
String line = str.replaceAll("[0-9]+/*.*[0-9]*", "8");
but to no avail.
但无济于事。
Any idea what the correct pattern should be?
知道正确的模式应该是什么吗?
采纳答案by Thomas
Try this expression: (?>-?\d+(?:[\./]\d+)?)
, keep in mind that in Java strings you need to escape the backslashes, i.e. you'd get "(?>-?\\d+(?:[\\./]\\d+)?)"
试试这个表达式:(?>-?\d+(?:[\./]\d+)?)
,记住在 Java 字符串中你需要转义反斜杠,即你会得到"(?>-?\\d+(?:[\\./]\\d+)?)"
Here's a breakdown of the expression:
这是表达式的细分:
The encloseing
(?>...)
is an atomic group to prevent catastrophic backtracking. For simple or short strings it would work without as well.-?
a potential minus for negative numbers\d+
any sequence of digits (at least one)(?:[\./]\d+)?
an optional non-capturing group consisting of either a dot (note that you don't need to escape it here, it's just for consistency) or a slash followed by at least one more digit.
封闭
(?>...)
是一个原子组,以防止灾难性的回溯。对于简单或短的字符串,它也可以工作。-?
负数的潜在负数\d+
任何数字序列(至少一个)(?:[\./]\d+)?
一个可选的非捕获组,由一个点(请注意,您不需要在此处对其进行转义,这只是为了保持一致性)或后跟至少一个数字的斜杠组成。
Update
更新
If you don't want to replace "numbers" like .1234
, 1234.
/1
or 5/
(a digit is missing either left or right), try this expression: (?>(?<![\d\./])-?\d+(?:(?:[\./]\d+)|(?![\d\./])))
如果您不想替换“数字”,例如.1234
,1234.
/1
或5/
(左或右缺少数字),请尝试以下表达式:(?>(?<![\d\./])-?\d+(?:(?:[\./]\d+)|(?![\d\./])))
Here's a breakdown again:
这里再次细分:
The encloseing
(?>...)
is an atomic group to prevent catastrophic backtracking. For simple or short strings it would work without as well.(?<![\d\./])
the match must not directly follow a digit, dot or slash - note that the not follow a digit constraint is needed to match at the start of the number, otherwise you'd match234
in.1234
-?
a potential minus for negative numbers\\d+
any sequence of digits (at least one)(?:(?:[\./]\d+)|(?![\d\./]))
the match must either have a dot or slash followed by at least one digit or must not be followed by a digit, dot or slash, this would match1.0
but not1.
- note that the not to be followed by a digit constraint is needed to prevent matching123
in1234.
封闭
(?>...)
是一个原子组,以防止灾难性的回溯。对于简单或短的字符串,它也可以工作。(?<![\d\./])
这场比赛一定不能直接跟随一个数字,圆点或斜线-注意,不是跟着一个数字的约束是需要匹配的号码的开始,否则你会匹配234
在.1234
-?
负数的潜在负数\\d+
任何数字序列(至少一个)(?:(?:[\./]\d+)|(?![\d\./]))
匹配必须有一个点或斜线后跟至少一个数字,或者后面不能跟一个数字、点或斜线,这将匹配1.0
但不匹配1.
- 请注意,需要后跟一个数字约束以防止匹配123
在1234.
回答by shybovycha
If you need to replace the whole number with just a single character, use this code:
如果您只需要用一个字符替换整个数字,请使用以下代码:
import java.io.*;
class Moo
{
public static void main(String[] args)
{
String vals[] = { "1.2", "-3.14", "100500" };
for (String s : vals)
System.out.println(s.replaceAll("(-)?\d+(\.\d*)?", "x"));
}
}
But if you need to replace each digit, you should use different regex, like this one: "\\d"
.
但是如果你需要替换每个数字,你应该使用不同的正则表达式,比如这个:"\\d"
.
See the demo.
请参阅演示。
回答by Highly Irregular
You've forgotten to escape the . character. Other than that, your pattern looks good to me.
你忘记了逃离 . 特点。除此之外,你的模式对我来说看起来不错。
String line = str.replaceAll("[0-9]+/*\.*[0-9]*", "8");
If that still doesn't work, please provide the cases that the expression isn't working correctly on.
如果这仍然不起作用,请提供表达式无法正常工作的情况。