简体   繁体   English

固定字符串的正则表达式

[英]Regular expression for fixed string

I have following string. 我有以下字符串。

?page=1&sort=desc&param=5&parm2=25

I need to check whether the enter string url is in strict format except variable value. 我需要检查输入字符串url是否是严格格式,变量值除外。 Like page=, sort=, param=, param2. 像page =,sort =,param =,param2。

Please suggest regular expression. 请建议正则表达式。 Thanks 谢谢

You should use parse_str and check if all the parameters you wanted are set with isset . 您应该使用parse_str并检查所需的所有参数是否都设置为isset Regex is not the way to go here. 正则表达式不是去这里的方式。

Maybe this : 也许这个:

\?page=\d+&sort=.+&param=\d+&param2=\d+

which translates to : 这意味着:

?page= followed by any digit repeated 1 or more times ?page = followed by any digit repeated 1 or more times

&sort= followed by any character repeated 1 or more times &sort = followed by any character repeated 1 or more times

&param= followed by any digit repeated 1 or more times &param = followed by any digit repeated 1 or more times

&param2= followed by any digit repeated 1 or more times &param2 = followed by any digit repeated 1 or more times

I think Alin Purcaru 's suggestion is better 我认为Alin Purcaru的建议更好

EDIT: 编辑:

(\?|&)(page=[^&]+|sort=[^&]+|param=[^&]+|parm2=[^&]+)

This way the order doesn't matter 这样顺序无关紧要

If you care about the order of parameters, something like this: 如果你关心参数的顺序,就像这样:

\?page=[^&]+&sort=[^&]+param=[^&]+param2=[^&]+$

But Alin Purcaru is right - use the parse_str function already written to do this 但是Alin Purcaru是对的 - 使用已经编写的parse_str函数来执行此操作

You could use the following regx /\\?page=[^&]*sort=[^&]*param=[^&]*param2=/` to match: 您可以使用以下regx / \\?page = [^&] * sort = [^&] * param = [^&] * param2 = /`来匹配:

if (preg_match("/\?page=([^&]*)sort=([^&]*)param=([^&]*)param2=([^&]*)/i", $inputstr, $matches))
{
   echo "Matches:";
   print_r($matches);     // matches will contain the params

}
else
   echo "Params nor found, or in wrong order;

正则表达式将是^\\?([\\w\\d]+=[\\w\\d]+(|&))*$只要您的值是Alpha数字,但也许您想看看过滤器是否你想验证一个网址http://www.php.net/manual/en/book.filter.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM