Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用正则表达式提取内容_Php_Regex - Fatal编程技术网

Php 使用正则表达式提取内容

Php 使用正则表达式提取内容,php,regex,Php,Regex,我有以下句子:enum'active'、'disabled'、'deleted'。我想要的是一个包含以下内容的阵列: array( [0]=>'active', [1]=>'disabled', [2]=>'deleted' ) 如何实现这一点?在给定字符串的情况下,类似于此正则表达式的东西应该可以工作 $sentence = "enum('active','disabled','deleted')"; preg_match_all("/'([^']*

我有以下句子:enum'active'、'disabled'、'deleted'。我想要的是一个包含以下内容的阵列:

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+?)'

比赛结果中是否应该包括报价?