Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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,我正在努力匹配带有if char exists regex查询的短语。我希望能够扫描此字符串: teamplayerID=5432&groupplayerID=2345 只需下车teamplayerID=5432。有时&groupplayerID存在,有时不存在 我试过: /teamplayerID=(*)(&?)/ 在 但是上面的代码似乎选择了整个字符串 问题: 如果正则表达式检测到&并且仅获取我想要的内容,但即使不存在&也选择我想要的内容,如何使其停止。您需要使用[^&]*: 见

我正在努力匹配带有if char exists regex查询的短语。我希望能够扫描此字符串:

teamplayerID=5432&groupplayerID=2345
只需下车
teamplayerID=5432
。有时&groupplayerID存在,有时不存在

我试过:

/teamplayerID=(*)(&?)/

但是上面的代码似乎选择了整个字符串

问题:
如果正则表达式检测到
&
并且仅获取我想要的内容,但即使不存在
&
也选择我想要的内容,如何使其停止。

您需要使用
[^&]*

[^&]*
模式匹配0个或更多出现的字符,而不是
&


如果ID始终是一组数字,只需使用
\d*
(0+个数字)即可。

您需要使用
[^&]*

[^&]*
模式匹配0个或更多出现的字符,而不是
&


如果ID始终是一组数字,只需使用
\d*
(0+个数字)即可。

如果Teamplayer始终是一个数字,则可以使用\d+

teamplayerID=(\d+)

如果它不总是一个数字,您可以使用\w+,它将同时匹配数字和字母

teamplayerID=(\w+)



非正则表达式总是有可能的

$str = "teamplayerID=54";

$pos1 = strpos($str, "=")+1;
$pos2 = strpos($str, "&", $pos1);
if($pos2 == 0) $pos2 = strlen($str);
echo substr($str, $pos1, $pos2-$pos1);
查找
=
另存为pos1。
=
之后查找
&
,并另存为pos2。
如果没有
&
请使用strlen($str)作为pos2。
子串pos1和长度(pos2-pos1)


编辑;忘记了groupID并不总是在那里。

如果Teamplayer总是一个数字,您可以使用\d+

teamplayerID=(\d+)

如果它不总是一个数字,您可以使用\w+,它将同时匹配数字和字母

teamplayerID=(\w+)



非正则表达式总是有可能的

$str = "teamplayerID=54";

$pos1 = strpos($str, "=")+1;
$pos2 = strpos($str, "&", $pos1);
if($pos2 == 0) $pos2 = strlen($str);
echo substr($str, $pos1, $pos2-$pos1);
查找
=
另存为pos1。
=
之后查找
&
,并另存为pos2。
如果没有
&
请使用strlen($str)作为pos2。
子串pos1和长度(pos2-pos1)


编辑;忘记了groupID并不总是在那里。

我想你必须发布一些字符串示例,这些字符串不能像你期望的那样工作。teamplayer总是一个数字吗?你试图解决错误的问题。使用PHP函数,问题就消失了。我想你将不得不发布一些字符串示例,这些字符串不能像你期望的那样工作。teamplayer总是一个数字吗?你试图解决错误的问题。使用PHP函数,问题就消失了。哇,这是一个快速有效的解决方案。12分钟内无法接受。:)哇,这是一个快速有效的解决方案。12分钟内无法接受。:)