java Regex: replace all numerical values with one number

青春壹個敷衍的年華 提交于 2019-12-12 18:28:18

问题


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 been 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?


回答1:


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:

  1. The encloseing (?>...) is an atomic group to prevent catastrophic backtracking. For simple or short strings it would work without as well.

  2. -? a potential minus for negative numbers

  3. \d+ any sequence of digits (at least one)

  4. (?:[\./]\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:

  1. The encloseing (?>...) is an atomic group to prevent catastrophic backtracking. For simple or short strings it would work without as well.

  2. (?<![\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 match 234 in .1234

  3. -? a potential minus for negative numbers

  4. \\d+ any sequence of digits (at least one)

  5. (?:(?:[\./]\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 match 1.0 but not 1. - note that the not to be followed by a digit constraint is needed to prevent matching 123 in 1234.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7737946/java-regex-replace-all-numerical-values-with-one-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!