我如何在 PHP 中使用正则表达式来匹配整个字符串中的给定组而不是在第一次匹配时停止

How can i use a regular expression in PHP that matches a given group in the whole string instead of stops at first match

我有以下表达式:

[\?&]([\w-]+)=([\w=.|()+%-]+)

我正在尝试验证以下查询字符串:

?tn_cid=280&tn_fk_specifications_planten_planttype=Groene%20kamer+planten|hangplanten|tuinplanten&tn_fk_ae-waterbehoefte=Weinig%20(2x%20per%20maand)&tn_fk_specifications_planten_plantheight=30%20-%2035%20cm&tn_fk_ae-hoogte-plant=Kleine%20planten%20(tot%2040%20cm)&tn_fk_ae-prijs=16.99-16.99

但是我的表达式只匹配字符串的第一部分然后就停止了:

?tn_cid=280

如何让表达式继续匹配每个组(即?key=value 或 &key=value)直到完整字符串结束?

您可以重复整个表达式,也可以省略捕获组以获得完整匹配:

(?:[?&][\w-]+=[\w=.|()+%-]+)+

Regex demo

或以 ? 开头,并可选择用 &

重复该部分
\?[\w-]+=[\w.|()+%-]+(?:&[\w-]+=[\w.|()+%-]+)*

Regex demo