首页 > 解决方案 > 允许在两个双引号正则表达式之间使用连字符

问题描述

我创建了正则表达式来捕捉双引号之间的单词。例子

string = Hi[pause="x-weak"]Parth[pause="weak"]How[pause="medium"]are[pause="strong"]You[pause="x-strong"]tell me

regex = \[\h*pause\h*=\h*"\h*(\w+)\h*"\h*]

它只是匹配[pause="weak"]。我需要它将所有标签与pause.

标签: phpregexregex-group

解决方案


你可以使用

\[[^][]*pause="(?P<type>[^"]+)"

这也将赶上type,请参阅regex101.com 上的演示


PHP这可能是

<?php

$string = 'Hi[pause="x-weak"]Parth[pause="weak"]How[pause="medium"]are[pause="strong"]You[pause="x-strong"]tell me';
$regex = '~\[[^][]*pause="(?P<type>[^"]+)"~';

if (preg_match_all($regex, $string, $matches)) {
    print_r($matches);
}
?>

在 ideone.com 上查看演示


推荐阅读