繁体   English   中英

用限制替换所有非数字

[英]Replace all non digits with a restriction

我正在尝试从表达式中提取第一个数字:

这是我的一段时间代码:

String[] strArray = input.split("\\+ ");
double[] numbers = getNumbersFromString();

public static double[] getNumbersFromString() {
   double[] numbers = new double[strArray.length];
   for (int i = 0; i < strArray.length; i++) {
            numbers[i] = Double.parseDouble(strArray[i].replaceAll("\\D", ""));
   }

   return numbers;
}

输入及其预期输出:

  • Z = 4x1 + 3x2 + 6x3 // 4 3 6
  • Z = 24x1 + 33x2 + 68x3 // 24 33 68
  • Z = 412x1 + 309x2 + 612x3 // 412309612
  • Z = 4329x1 + 3901x2 + 6716x3 // 4329 3901 6716

实际上,它正在删除,但除了第一个数字外,还会检索第二个数字。 例如(在第一种情况下):// 41 32 63,而应该仅为// 4 3 6。

我可以做类似"\\\\w = |x\\\\d", "" ,但是它仅适用于这种特定情况,我想要一个更通用的方法。

提前致谢。

编辑:

对于原始问题,我得到以下答案:

String input = "Z = 4329x1 + 3901x22 + 6716x3";
input = input.replaceAll("^\\D+", "");
double[] numbers = Pattern.compile("x\\d+\\D*")
                      .splitAsStream(input)
                      .mapToDouble(Double::parseDouble)
                      .toArray();

但是现在看来,有一件新的事情要做。 x1/x2/x3 ..或任何此类输入之前没有数字的输入应替换为数字“ 1”。

一些投入及其各自的预期产出:

  • Z = x11 + x2 + x90 // 1 1 1
  • Z = 2x1 + 2x4 + x9 // 2 2 1

顺便说一下,我做了这个正则表达式: (?<!\\d)x\\d+

然后,我将代码修改为:

return Pattern.compile("x\\d+\\D*")
                .splitAsStream(input.replaceAll("(?<!\\d)x\\d+","1").replaceAll("^\\D+", ""))
                .mapToDouble(Double::parseDouble)
                .toArray();

但是它返回了我`java.lang.NumberFormatException:对于输入字符串:“ 1 + 3”。

附言:无论新旧情况,它都应该起作用。

这应该按要求工作:

String s = "Z = 4329x1 + 3901x22 + 6716x3";
String[] split = s.replaceAll("^\\D+", "").split("x\\d+\\D*");
System.out.println(Arrays.toString(split)); //[4329, 3901, 6716]

使用流,您可以执行以下操作来获取双精度数组:

String input = "Z = 4329x1 + 3901x22 + 6716x3";
input = input.replaceAll("^\\D+", "");
double[] numbers = Pattern.compile("x\\d+\\D*")
                          .splitAsStream(input)
                          .mapToDouble(Double::parseDouble)
                          .toArray();

编辑

要也接受x1 + x2表达式,当split返回的字符串为空时,可以将默认值设置为1 (并稍稍修改正则表达式):

String input = "Z = x1 + x2 + 6716x3";
input = input.replaceAll("^[^x\\d]+", "");
double[] numbers = Pattern.compile("x\\d+[^x\\d]*")
        .splitAsStream(input)
        .mapToDouble(s -> s.isEmpty() ? 1d : Double.parseDouble(s))
        .toArray();

重新编辑

手动在x前面添加缺失的1:

String input = "Z = x1 + x2 + 6716x3 + x4";
input = input.replace(" x", " 1x")
             .replaceAll("^[^x\\d]+", "");

double[] numbers = Pattern.compile("x\\d+[^x\\d]*")
        .splitAsStream(input)
        .mapToDouble(Double::parseDouble)
        .toArray();

由于您的模式是一个数字,后跟一个x ,因此可以像这样使用positive lookahead

正则表达式: \\d+(?=x)

说明:

  • (?=x)向前看并检查x是否存在。 如果是,则\\d+匹配。

Regex101演示

注意:如有必要, 使用双转义\\\\d

您可以在单个正则表达式中进行整个抓取(根据您的示例,我假设您感兴趣的每一行)-

(?:=|\+)\s*(\d+)

它与=+匹配,跳过任何空格然后捕获数字。 使其全局,它将捕获整个行。 结果将在捕获组数组中。 (对Java的讲得不够好,无法从头顶上确切地告诉您如何做)。

在regex101处检查 (注意-样本一次显示所有行-您必须一个接一个地完成它们。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM