用于提取带有可选点的前面数字的正则表达式



>我正在寻找一个正则表达式。

文本示例

1 Match
1.1 Match
45.67.21234.3 Match
1 Does not match1
12. Does not match

提取/匹配的值应为:

1
1.1
45.67.21234.31

这些不应与:

1 Does not match1 // no match because of an additional digit in the text
12. Does not match // no match because of the dot after 12

到目前为止,我的正则表达式如下所示:

(d+.)+d

但这与第一个条目不匹配。

使用

(d+.)+d 不会匹配第一个条目,因为使用量词+它必须至少匹配 1 次数字和一个点。

您可以做的是使用锚^来断言字符串的开头,并使用模式匹配数字,然后重复匹配点和数字零次或多次,以便您也匹配第一个条目。

匹配后,请确保数字后面没有非空格字符。如果后面不能再有数字,您可以使用额外的负数前瞻。

^d+(?:.d+)*(?!S)(?!.*d)

在爪哇中:

String regex = "^\d+(?:\.\d+)*(?!\S)(?!.*\d)";

正则表达式演示

解释

  • ^ 字符串的开头
  • d+(?:.d+)*匹配 1+ 位数字
  • ,后跟重复模式以匹配点和 1+ 位数字
  • (?!S) 负面的前瞻性检查左侧的内容不是非空格字符
  • (?!.*d) 负数 前瞻检查右侧的内容不包含数字
您可以使用

的正则表达式是:

^((d+.)*d+) D*$

捕获组 1 将容纳您的比赛的位置。

解释:

^              # Start of the String
 (             # Open capture group 1:
  (d+.)      #  One or more digits, followed by a dot
         *     #  Repeated 0 or more times
          d+  #  Followed by 1 or more digits
 )             # Closing capture group 1
               # Followed by a space
   D*         # Followed by 0 or more non-digits
$              # Followed by the end of the String

^$将使我们查看整个字符串。D*将确保空格后的子字符串中没有任何数字。(d+.)*后面的d+确保始终有一个前导数字,前面有一个或多个#.(其中#是非负数)。

要提取此值,您可以将此正则表达式与String.matches一起使用,并像这样.replaceFirst

// TODO: Give proper method name
String test(String str){
  String regex = "^((\d+\.)*\d+) \D*$";
  if(str.matches(regex))
    return str.replaceFirst(regex, "$1");
    // The `$1` will leave just the match of the first capture group,
    // removing everything else we don't need
  else
    return null;
}

在线试用。

如果单个数字后面没有任何空格(即 "123" ) 也应该匹配,可以通过将\D*$正则表达式更改为 ( \D*)?$ 来对正则表达式进行微小修改,以便空格变为可选。

在线试用。

我们可以尝试对每一行使用以下正则表达式模式:

^(?!D*d[^0-9.]+d).*bd+(?:.d+)?(?=\s|$).*$

解释:

^                            from the start of the line
    (?!D*d[^0-9.]+d)      assert that two (or more) separate numbers
                             do not occur in the line
    .*                       then consume anything, up to
    bd+(?:.d+)?          an integer, or complete decimal
    (?=\s|$)                where either a space or the end of the line follows
    .*                       then consume anything, up to
$                            the end of the line

下面是使用此模式的 Java 代码:

String line = "45.67.21234.3";
String pattern = "^(?!\D*\d[^0-9.]+\d).*\b\d+(?:\.\d+)?(?=\s|$).*$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
    System.out.println("match");
}
else {
    System.out.println("no match");
}

我已经针对您的所有输入对其进行了测试,它似乎正在工作。