且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在线找到具有两个不同参数的多个匹配绑定

更新时间:2023-02-14 16:55:50

问题是您的第二个正则表达式:

The problem is that your second regex:

with (.*) in (.*)

匹配这两行

with a partridge in a pear tree
with a partridge for Christmas in a pear tree

首先,它将选择"partridge"和梨树"作为两个参数.在第二个中,它将选择圣诞节的隔离区"和一棵梨树"作为参数.由于您的第一个正则表达式也与第二行匹配,因此确实可以找到多个绑定.

In the first instance, it'll pick up "partridge" and "a pear tree" as the two arguments. In the second, it will pick up "partridge for Christmas" and "a pear tree" as the arguments. Since your first regex also matches that second line, it is indeed finding multiple bindings.

您可以使用其他正则表达式.例如,如果您想选择一个完整的单词而不包含任何空格,请尝试使用(\S*)而不是(.*). .匹配任何内容,包括空格.有关正则表达式的更多信息此处.

You could use a different regex. For instance, if you want to pick out a whole word and not have any whitespace included, try (\S*) instead of (.*). That . matches anything, including spaces. More on regex here.