gpt4 book ai didi

java - 如何仅匹配前面/后面没有字母字符的数字?

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:50 25 4
gpt4 key购买 nike

我对正则表达式相当陌生。在 Java 中,我试图找到模式:

(<numeric> (completes|completing)) OR ((completes|completing) <numeric>)

我使用的正则表达式是

([\d]*(.[\d]+))?\s*(completes|completing)\s*([\d]*(.[\d]+))?

它将匹配以下模式:

2.29 completes
completes 2.29
2.29 completing
completing 2.29

但是,输入文本的数字旁边可能有一个字母字符。在这些情况下,输出将是:

x3.25 completes 2.29 (match: completes 2.29)
2.29 completes 3.25x (match: 2.29 completes)
x3.25 completing 2.29 (match: completing 2.29)
2.29 completing 3.25x (match: 2.29 completing)

这是示例数据

n16 2.00/2.50  stg live completes 6.5
v/f -30 cso completing .006
m16 1.95p live completing 1
m16 1.95p live completing 1 again and out
n16 1.75/2.50 stg live completing 2.6
v16 2.75c x2.39 completing 9.9, 650x go
v16 2.75c x2.39 completing 9.9 again, 900 go
q 2.0 p vs 2.25/2.50 cs live .026 completing
q 2.00 p vs 2.25/2.50 c 2.21 completes 500x .026
v16 2.75c x2.39 completing 9.9 again and out, 1500x go
m16 1.90p live completing .25 and out
fh17 4.00/4.50cs x3.01 completing 4.9
z 2.00 p x2.89 completes .023 6 delta
fh17 4.00/4.50cs x3.01 completing 4.9 again, 500/month go
h17 4.00/6.00 cs x 2.99 completes 12.4 , 18 delta
h17 4.00/6.00 cs x 2.99 completes 12.4 , 18 delta , 500x , 12.3/12.5 follow
z 2.00 p x2.89 completes .024
fh17 4.00/4.50cs x3.01 completing 5 now
f17-h17 2.50/3.00 ps x 3.01 completes 23.3 , 21 delta
h17 5.00 c x 2.99 completes 8.7
cal18 2.25/4.25 fen live completing 1.2, 200/month go and out

正则表达式如何满足此要求?感谢您的帮助。

最佳答案

描述

(?=x\s*[0-9.]+\s+complet).*?\K(?:complet(?:es|ing))\s*[0-9.]+|complet(?:es|ing)\s*[0-9.]+(?!\s*x|[0-9])|[0-9.]+\s*complet(?:es|ing)

Regular expression visualization

此正则表达式将执行以下操作:

  • 将匹配completescompleting以及前面或后面的数字
  • 如果数字前面有 x,则排除前面的数字
  • 排除后面跟着 x 的尾随数字

注意:将数字断言从 [0-9]+(?:\.[0-9])? 更改为 [0-9.]+,因为您的示例文本似乎已经过验证并且格式更易于阅读。

示例

现场演示

https://regex101.com/r/oA7fU4/1

示例文本

n16 2.00/2.50  stg live completes 6.5
v/f -30 cso completing .006
m16 1.95p live completing 1
m16 1.95p live completing 1 again and out
n16 1.75/2.50 stg live completing 2.6
v16 2.75c x2.39 completing 9.9, 650x go
v16 2.75c x2.39 completing 9.9 again, 900 go
q 2.0 p vs 2.25/2.50 cs live .026 completing
q 2.00 p vs 2.25/2.50 c 2.21 completes 500x .026
v16 2.75c x2.39 completing 9.9 again and out, 1500x go
m16 1.90p live completing .25 and out
fh17 4.00/4.50cs x3.01 completing 4.9
z 2.00 p x2.89 completes .023 6 delta
fh17 4.00/4.50cs x3.01 completing 4.9 again, 500/month go
h17 4.00/6.00 cs x 2.99 completes 12.4 , 18 delta
h17 4.00/6.00 cs x 2.99 completes 12.4 , 18 delta , 500x , 12.3/12.5 follow
z 2.00 p x2.89 completes .024
fh17 4.00/4.50cs x3.01 completing 5 now
f17-h17 2.50/3.00 ps x 3.01 completes 23.3 , 21 delta
h17 5.00 c x 2.99 completes 8.7
cal18 2.25/4.25 fen live completing 1.2, 200/month go and out

示例匹配

[0] => completes 6.5
[1] => completing .006
[2] => completing 1
[3] => completing 1
[4] => completing 2.6
[5] => completing 9.9
[6] => completing 9.9
[7] => .026 completing
[8] => 2.21 completes
[9] => completing 9.9
[10] => completing .25
[11] => completing 4.9
[12] => completes .023
[13] => completing 4.9
[14] => completes 12.4
[15] => completes 12.4
[16] => completes .024
[17] => completing 5
[18] => completes 23.3
[19] => completes 8.7
[20] => completing 1.2

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
x 'x'
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
----------------------------------------------------------------------
complet 'complet'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
\K 'K'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
complet 'complet'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
es 'es'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
ing 'ing'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
complet 'complet'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
es 'es'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
ing 'ing'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
x 'x'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
complet 'complet'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
es 'es'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
ing 'ing'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------

关于java - 如何仅匹配前面/后面没有字母字符的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37443725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com