Php 使用正则表达式提取内容
我有以下句子:enum'active'、'disabled'、'deleted'。我想要的是一个包含以下内容的阵列:Php 使用正则表达式提取内容,php,regex,Php,Regex,我有以下句子:enum'active'、'disabled'、'deleted'。我想要的是一个包含以下内容的阵列: array( [0]=>'active', [1]=>'disabled', [2]=>'deleted' ) 如何实现这一点?在给定字符串的情况下,类似于此正则表达式的东西应该可以工作 $sentence = "enum('active','disabled','deleted')"; preg_match_all("/'([^']*
array(
[0]=>'active',
[1]=>'disabled',
[2]=>'deleted'
)
如何实现这一点?在给定字符串的情况下,类似于此正则表达式的东西应该可以工作
$sentence = "enum('active','disabled','deleted')";
preg_match_all("/'([^']*)'/", $sentence, $matches);
print_r($matches[1]);
上述代码输出以下内容
Array
(
[0] => active
[1] => disabled
[2] => deleted
)
Regex解释道
' //Match opening quote.
( //Start capture.
[^']* //Match any characters but the end quote.
) //End capture.
' //Match closing quote.
( //Start capture.
' //Match opening quote.
[^']* //Match any characters but the end quote.
' //Match closing quote.
) //End capture.
更新:
一位评论员建议,也许你希望保留这些引文。如果是这样,下面的正则表达式将起作用
$s = "enum('active','disabled','deleted')";
preg_match_all("/('[^']*')/", $s, $matches);
print_r($matches[1]);
输出
Array
(
[0] => 'active'
[1] => 'disabled'
[2] => 'deleted'
)
Regex解释道
' //Match opening quote.
( //Start capture.
[^']* //Match any characters but the end quote.
) //End capture.
' //Match closing quote.
( //Start capture.
' //Match opening quote.
[^']* //Match any characters but the end quote.
' //Match closing quote.
) //End capture.
您可以使用此正则表达式:
'(\w+?)'
比赛结果中是否应该包括报价?