Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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 在这种情况下,如何获得正确的preg_match()结果_Php_Regex_Preg Match - Fatal编程技术网

Php 在这种情况下,如何获得正确的preg_match()结果

Php 在这种情况下,如何获得正确的preg_match()结果,php,regex,preg-match,Php,Regex,Preg Match,在这种情况下,我想从preg\u match()中获得结果 $regex = '/[A-Z][a-z][0-9]/i'; 获得以下结果的正确代码是什么 preg_match($regex, 'phpversion7') //return false preg_match($regex, 'Phpversion7') //return true preg_match($regex, 'Phpversion') //return false preg_match($regex, 'R

在这种情况下,我想从
preg\u match()
中获得结果

$regex = '/[A-Z][a-z][0-9]/i'; 
获得以下结果的正确代码是什么

preg_match($regex, 'phpversion7')  //return false

preg_match($regex, 'Phpversion7')  //return true

preg_match($regex, 'Phpversion')  //return false

preg_match($regex, 'R1985y2528')  //return true

preg_match($regex, 'R19852528')  //return false

简单的asign变量到每个preg_match()语句,该语句将存储一个布尔值不是很清楚您想要什么,但我想您需要如下内容:

/^[A-Z](?=.*[a-z])(?=.*\d)[a-zA-Z\d]+$/
此正则表达式匹配以大写字母开头且至少包含一个字母和至少一个数字的字符串

说明:

/               : regex delimiter
  ^             : start of string
  [A-Z]         : a capital letter
  (?=.*[a-z])   : lookahead, a small letter must be present in the string
  (?=.*\d)      : lookahead, a digit must be present in the string
  [a-zA-Z\d]+   : the whole string must contains only letters and digits.
  $             : end of string
/               : regex delimiter

你必须更清楚,你想要的结果是什么,你尝试了什么?@Nytrix这个怎么样。你可能仍然应该告诉我们
$regex
是什么(假设你将它作为一个变量)。。。第二,在什么条件下你想要结果?请试着用你提供的信息自己回答这个问题,你没有提供太多。