Regex -获取字符串内的所有匹配项


Regex - get all matches inside a string

我有一个这样的字符串:

$sContent = "t('Dashboard').' '.t('Settings Test').' '.t('My Test').' '.t('Other test')";

我需要的是一个正则表达式,它可以获得每个单独的t(")值,如:

t('Dashboard')
t('Settings Test')
...

在PHP中我使用:

$sRegExp = '/t'((.*)')/i';
preg_match_all($sRegExp, $sContent, $aResults);

我得到与主字符串相同的结果:

t('Dashboard').' '.t('Settings Test').' '.t('My Test').' '.t('Other test')

您可以使用:

$sRegExp = '/t'((.*?)')/

$sRegExp = '/t'(([^')]*)')/

好吧,让我们假设你永远不会在文本中不使用'。这应该有帮助。

preg_match_all('/t'(''(?<=t'('')([^'']*(?!=''')))''')/', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

好的,如果你想使用'符号,你必须用'转义它,然后尝试使用下面的模式

/t'(''(?<=t'('')(((?='''')''''''|[^''])*(?!=''')))''')/

我会尝试explode函数:

$sContent = "t('Dashboard').' '.t('Settings Test').' '.t('My Test').' '.t('Other test')";
$sContents = explode(".' '.", $sContent);