正则表达式:如何“退一步”

发布于 2024-10-08 23:17:19 字数 307 浏览 9 评论 0原文

我在编写产生此结果的正则表达式时遇到一些麻烦:

Mike1, misha1,2, miguel1,2,3,4,5, 6,7,18 和 Michea2,3

如何在正则表达式中后退并丢弃最后一个匹配项?也就是说,我需要在空格前加一个逗号才能不匹配。这就是我想出的...

\d+(,|\r)

Mike1, misha1,2, miguel1,2,3,4,5,6,7,18 、 和迈克尔2,3

I am having some trouble cooking up a regex that produces this result:

Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Michea2,3

How does one step back in regex and discard the last match? That is I need a comma before a space to not match. This what I came up with...

\d+(,|\r)

Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Micheal2,3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2024-10-15 23:17:19

您所询问的正则表达式功能称为正向后查找。但就你的情况而言,我认为你不需要它。试试这个:

\d+(?:,\d+)*

在您的示例中,这将匹配逗号分隔的数字列表,并排除名称和尾随逗号和空格。

下面是用 PHP 编写的一小段测试代码,用于验证您的输入:

<?php
$input = "Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Micheal2,3";
$matches = array();
preg_match_all('/\d+(?:,\d+)*/', $input, $matches);
print_r($matches[0]);
?>

输出:

Array
(
    [0] => 1
    [1] => 1,2
    [2] => 1,2,3,4,5,6,7,18
    [3] => 2,3
)

The regex feature you're asking about is called a positive lookbehind. But in your case, I don't think you need it. Try this:

\d+(?:,\d+)*

In your example, this will match the comma delimited lists of numbers and exclude the names and trailing commas and whitespace.

Here is a short bit of test code written in PHP that verifies it on your input:

<?php
$input = "Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Micheal2,3";
$matches = array();
preg_match_all('/\d+(?:,\d+)*/', $input, $matches);
print_r($matches[0]);
?>

outputs:

Array
(
    [0] => 1
    [1] => 1,2
    [2] => 1,2,3,4,5,6,7,18
    [3] => 2,3
)
诺曦 2024-10-15 23:17:19

我相信 \d+,(?!\s) 会做你想要的。 ?! 是一个 否定先行,仅匹配?! 后面的内容没有出现在搜索字符串的这个位置。

>>> re.findall(r'\d+,(?!\s)', 'Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Michea2,3')
['1,', '1,', '2,', '3,', '4,', '5,', '6,', '7,', '2,']

或者,如果您想匹配以逗号分隔的数字列表(不包括最后的逗号),请使用 \d+(?:,\d+)*

>>> re.findall(r'\d+(?:,\d+)*', 'Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Michea2,3')
['1', '1,2', '1,2,3,4,5,6,7,18', '2,3']

I believe \d+,(?!\s) will do what you want. The ?! is a negative lookahead, which only matches if what follows the ?! does not appear at this position in the search string.

>>> re.findall(r'\d+,(?!\s)', 'Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Michea2,3')
['1,', '1,', '2,', '3,', '4,', '5,', '6,', '7,', '2,']

Or if you want to match the comma-separated list of numbers excluding the final comma use \d+(?:,\d+)*.

>>> re.findall(r'\d+(?:,\d+)*', 'Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Michea2,3')
['1', '1,2', '1,2,3,4,5,6,7,18', '2,3']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文