简体   繁体   English

PHP正则表达式,带有特定字词_my_separator_

[英]PHP regular expression with a specific word _my_separator_

Please I have I want to use regex to preg_match this kind of string : 请让我使用正则表达式来preg_match这种字符串:

$liste = 'bla0bla-__my_separator_-01blabla';

I've tried : 我试过了 :

if(preg_match('/^[a-zA-Z0-9]_my_separator_[a-zA-Z0-9]$/', $liste))
     echo 'ok';
else echo 'not ok';

But this returns always not ok . 但这返回总是不好

Please masters any advise ? 请高手任何指教?

PS : I think that the problem is the _ and the - that what I've tried does not support ! PS:我认为问题是_-我尝试过的内容不支持!

Thanks in advance. 提前致谢。

将您的正则表达式模式更改为此:

'/^[a-zA-Z0-9_\-]+?_my_separator_[a-zA-Z0-9_\-]+?$/'

If you're only trying to determine if a static substring exists in a string you should use strpos() : 如果仅尝试确定字符串中是否存在静态子字符串,则应使用strpos()

if (strpos('_my_separator_', $liste) !== false) {
  echo 'ok';
}

You're missing the repeater + and - and _ in your whitelists: 您在白名单中缺少中继器+-_

/^[a-zA-Z0-9\\-_]+_my_separator_[a-zA-Z0-9\\-_]+$/

Your orignal regex would match things like: 您的原始正则表达式将匹配以下内容:

A_my_seperator_B
0_my_seperator_C

but not: 但不是:

AB_my_seperator_C

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

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