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".
I've b开发者_开发问答een fiddling about with Regex in Java, with things like this:
String line = str.replaceAll("[0-9]+/*.*[0-9]*", "8");
but to no avail.
Any idea what the correct pattern should be?
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+)?)"
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.
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\./])))
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.
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"
.
See the demo.
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.
精彩评论