【发布时间】:2017-05-05 17:36:10
【问题描述】:
如何检索非字母数字或下划线与句点之间的所有字符串?例如,对于下面的字符串,获取[sources_1st,sources]。 https://stackoverflow.com/a/10596688/1032531 是一个类似的问题,但似乎对我不起作用。
<?php
function check($pattern,$str)
{
echo($pattern.'<br>');
preg_match($pattern, $str, $matches);
echo('<pre>'.print_r($matches,1).'</pre>');
}
$str='fullname("protocol",coalesce(sources_1st.protocol,sources.protocol))';
echo($str.'<hr>');
check('/[^.]+\.[^.]+$/',$str);
check('/[\w]\..*$/',$str);
check('/[^\w]\..*$/',$str);
check('/\w\..*$/',$str);
check('/\w+(?=.*\.)$/',$str);
check('/\w+(?=.*\\.)$/',$str);
check('/\b[^ ]+\.$/',$str);
check('/\b[^ ]+\\.$/',$str);
check('/.*?(?=\.)$/',$str);
输出:
fullname("protocol",coalesce(sources_1st.protocol,sources.protocol))
--------------------------------------------------------------------------------
/[^.]+\.[^.]+$/
Array
(
[0] => protocol,sources.protocol))
)
/[\w]\..*$/
Array
(
[0] => t.protocol,sources.protocol))
)
/[^\w]\..*$/
Array
(
)
/\w\..*$/
Array
(
[0] => t.protocol,sources.protocol))
)
/\w+(?=.*\.)$/
Array
(
)
/\w+(?=.*\.)$/
Array
(
)
/\b[^ ]+\.$/
Array
(
)
/\b[^ ]+\.$/
Array
(
)
/.*?(?=\.)$/
Array
(
)
【问题讨论】:
-
您能给我们举个例子,说明输入的样子和输出的样子吗?
-
@jrn 输入为
'fullname("protocol",coalesce(sources_1st.protocol,sources.protocol))',输出为['sources_1st','sources']
标签: php regex preg-match pcre