Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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_Preg Match_Preg Match All - Fatal编程技术网

在php中,从字符串和特定子字符串的开头检测空格或括号

在php中,从字符串和特定子字符串的开头检测空格或括号,php,regex,preg-match,preg-match-all,Php,Regex,Preg Match,Preg Match All,我想从主字符串中检测子字符串的出现情况,但子字符串可以从空格或(开始时打开括号或结束时关闭括号)或开始。尾端圆点 我想写一个正则表达式来检测所有4个条件并计算php中的出现次数 编辑-感谢JE SUIS,我可以检查开始和结束条件,但只有当$mainspring=“php”在“this is php”上不起作用时,它才起作用。 它不会忽略php之前的单词 有没有办法在比赛前和比赛后忽略所有的线 $mainString = "i love java but i want to learn p

我想从主字符串中检测子字符串的出现情况,但子字符串可以从空格或(开始时打开括号或结束时关闭括号)或开始。尾端圆点

我想写一个正则表达式来检测所有4个条件并计算php中的出现次数 编辑-感谢JE SUIS,我可以检查开始和结束条件,但只有当$mainspring=“php”在“this is php”上不起作用时,它才起作用。 它不会忽略php之前的单词

有没有办法在比赛前和比赛后忽略所有的线

    $mainString = "i love java but i want to learn php and this keyword ofphp should not be count because there is no space before php but this could be count (php)";
    $keyword = "php";
    $match =preg_match('/^[(\s]?'.$keywords.'[.)]?$/', $mainString);
    var_dump($match);
我不知道如何为这种情况编写正则表达式 任何帮助都将不胜感激

谢谢

这就可以了:

preg_match('/^[(\s]$keyword[.)]$/', $mainString, $match);
其中:

/           : regex delimiter
  ^         : begin of string
  [(\s]     : an open parenthesis or a space
  $keyword  : the keyword to find
  [.)]      : a dot or a close parenthesis
  $         : end of string
/           : regex delimiter
请注意,preg_match的结果是一个布尔值,匹配在第三个参数中

根据评论编辑:

如果要在字符串中间匹配<代码> $关键字<代码>,只需删除锚点<代码> ^ < /代码>和<代码> $< /代码>:

preg_match('/[(\s]$keyword[.)]/', $mainString, $match);
如果前后有一些十六进制字符:

preg_match('/[0-9a-fA-F][(\s]$keyword[.)][0-9a-fA-F]/', $mainString, $match);
如果不是强制性的:

preg_match('/[0-9a-fA-F]?[(\s]$keyword[.)][0-9a-fA-F]?/', $mainString, $match);

$match=preg_match(“/^\($keyword\)/”,$mainString)
将匹配圆括号内的
$keyword
。您可以进一步处理此问题,以添加更多条件作为角色类。谢谢您的帮助。。但是我想在它里面再加一件事,“在$keyword之前可能会有另一个文本,或者在关键字之后,所以我可以把regex改成这样”[0-9a-fA-F]/^[(\s]$keyword[)]$/[0-9a-fA-F]”,因为当我把一个关键字放进主字符串中时,你的regex就可以工作了……等着你吧?@Swap IOS安卓:De rien。(我是法国人)我住在巴黎……迷人。