0

当 OR 语句中的第二个值匹配时,我的正则表达式返回一个空白值,如果找到第二个匹配项,我希望它忽略第一个匹配项

preg_match('/>\s+(?:<input type="submit" value="(.*?)" \/>|<p class="no">(.*?)<\/p>)\s+<\/form>/', $string, $matches);

如果第一项匹配:

Array
(
    [0] => <input type="submit" value="Go" />
    [1] => Go
)

如果第二项匹配:

Array
(
    [0] => <p class="no">Value</p>
    [1] => 
    [2] => Value
)

如何让它返回 [1] 而不是 [2] 中的第二个项目值?

4

1 回答 1

3

不要使用(?:来启动选项,而是使用(?|[ docs ]

于 2012-07-19T17:31:35.583 回答