Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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,我需要提取这些值​​一个条件(WHERE)并做了一个正则表达式,但我无法得到值​​没错 //Patherns $regex = "/([a-zA-Z_]+)\s([\<\=\>\s]{0,4})\s+(\".*\")/"; //values ​​to be extracted $string = 'idCidade >= "bla" OR idEstado="2" and idPais="3"'; //regex function preg_match_all(

我需要提取这些值​​一个条件(WHERE)并做了一个正则表达式,但我无法得到值​​没错

//Patherns
$regex  = "/([a-zA-Z_]+)\s([\<\=\>\s]{0,4})\s+(\".*\")/";

//values ​​to be extracted
$string = 'idCidade >= "bla" OR idEstado="2" and idPais="3"'; 

//regex function
preg_match_all(
    $regex,
    $string,
    $output
);

//displays the result
echo '<pre>';print_r($output);



//incorrect output
Array
(
   [0] => Array
    (
        [0] => idCidade >= "bla" OR idEstado="2" and idPais="3"
    )

   [1] => Array
    (
        [0] => idCidade 
    )

   [2] => Array
    (
        [0] => >= 
    )

   [3] => Array
    (
        [0] => "bla" OR idEstado="2" and idPais="3"
    )
)

您的错误可能是匹配太多的
*
。您需要在“ungreedy”中添加一个问号:
*?

不过,我建议使用以下正则表达式:

[1] => Array
    (
        [0] => 
        [1] => OR
        [2] => and
    )

[2] => Array
    (
        [0] => idCidade
        [1] => idEstado
        [2] => idPais
    )

[3] => Array
    (
        [0] => >=
        [1] => =
        [2] => =
    )

[4] => Array
    (
        [0] => "bla"
        [1] => "2"
        [2] => "3"
    )

我还使它适用于SQL兼容的字符串和小数。但这对正则表达式来说只是一个边缘工作。建议使用真正的解析器。(虽然我不知道您的用例。)

您的错误可能是匹配太多的
*
。您需要在“ungreedy”中添加一个问号:
*?

不过,我建议使用以下正则表达式:

[1] => Array
    (
        [0] => 
        [1] => OR
        [2] => and
    )

[2] => Array
    (
        [0] => idCidade
        [1] => idEstado
        [2] => idPais
    )

[3] => Array
    (
        [0] => >=
        [1] => =
        [2] => =
    )

[4] => Array
    (
        [0] => "bla"
        [1] => "2"
        [2] => "3"
    )

我还使它适用于SQL兼容的字符串和小数。但这对正则表达式来说只是一个边缘工作。建议使用真正的解析器。(虽然我不知道您的用例。)

试试这个。这将输出您需要的确切结果


试试这个。这将输出您需要的确切结果


在我的工作中,我使用这个正则表达式来处理传递给现有API的参数,这个人可能会寻求指定字段、值​​和操作员。因此,我必须验证数据,并验证字段是否已通知并被授权进行搜索。ex.api.com/?search=标题如blabla和id>0我忘记了运算符“like”并更改了正则表达式,我所做的更改是否正确$regex='/(或| AND)?\s*(\w+)\s*([!]+| like)\s*(“[^”]*“\'[^\']*\'.\d+)/i';如果您需要
like
操作符,则将regex的中间部分更改为
([!]+| like)
我在工作中使用此regex处理传递给现有API的参数,该人员可能会寻求指定字段、值​​和运算符。因此,我必须验证数据并验证字段是否已通知并被授权搜索。例如:api.com/?search=title like blabla和id>0我忘记了运算符“like”并更改了正则表达式,我正确地进行了更改?$regex='/(或| and)?\s*(\w+)\s*([!]+| like)\s*(“[^”]*“\\'[^\']*“\\\\\\\'[^\']*\\\\\'\d+/I';如果需要
LIKE
操作符,则将正则表达式的中间部分更改为
([!]+| LIKE)
[1] => Array
    (
        [0] => 
        [1] => OR
        [2] => and
    )

[2] => Array
    (
        [0] => idCidade
        [1] => idEstado
        [2] => idPais
    )

[3] => Array
    (
        [0] => >=
        [1] => =
        [2] => =
    )

[4] => Array
    (
        [0] => "bla"
        [1] => "2"
        [2] => "3"
    )
<?php  //Patherns
$regex  = '/([a-zA-Z_]+)\s*([>=<]*)\s*"([^"]*)"\s*(or|and)*/i';

//values to be extracted
$string = 'idCidade >= "bla" OR idEstado="2" and idPais="3"';

//regex function
preg_match_all(
    $regex,
    $string,
    $output
);

//displays the result
echo '<pre>';print_r($output);