开发者

java Regex: replace all numerical values with one number

开发者 https://www.devze.com 2023-04-12 16:33 出处:网络
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 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:

  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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消