Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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,在SO中尝试一个问题时,我尝试编写与字符串中应包含的三个字符相匹配的正则表达式 我正在跟踪答案 输出应该是系统的和神秘的,但我也得到了综合 为什么这样?我做错了什么 **我不想要新的解决方案:) 您可以执行以下操作: $wordlist = 'systematic,gear,synthesis,mysterious'; $words = explode(',', $wordlist); foreach($words as $word) { if (preg_match('~(?=[

在SO中尝试一个问题时,我尝试编写与字符串中应包含的三个字符相匹配的正则表达式

我正在跟踪答案


输出应该是系统的和神秘的,但我也得到了综合

为什么这样?我做错了什么

**我不想要新的解决方案:) 您可以执行以下操作:

$wordlist = 'systematic,gear,synthesis,mysterious';
$words = explode(',', $wordlist);

foreach($words as $word) {
    if (preg_match('~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~', $word))
        echo '<br/>' . $word;
}
//or
$res = preg_grep('~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~', $words);
print_r($res);
$wordlist='系统、装备、合成、神秘';
$words=分解(“,”,$wordlist);
foreach($words作为$word){
if(preg_match(“~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~”,$word))
回显“
”。$word; } //或 $res=preg_grep(“~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~”,$words); 印刷品(港币);;
为了测试字符串中是否存在字符,我使用
(?=[^s]*s)

[^s]*s
表示所有不是“s”的零次或多次,以及“s”的事物。
(?=…)
是一个先行断言,表示“后跟”。这只是一个检查,一个前瞻在匹配结果中不提供字符,但是这个特性的主要优点是可以多次检查相同的子字符串

你的模式有什么问题

/^(^s | ^m | ^e)/
将只提供以“s”或“m”或“e”开头的单词,因为
^
是一个锚点,表示“字符串的开头”。换句话说,您的模式与
/^([sme])/
相同。您可以执行以下操作:

$wordlist = 'systematic,gear,synthesis,mysterious';
$words = explode(',', $wordlist);

foreach($words as $word) {
    if (preg_match('~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~', $word))
        echo '<br/>' . $word;
}
//or
$res = preg_grep('~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~', $words);
print_r($res);
$wordlist='系统、装备、合成、神秘';
$words=分解(“,”,$wordlist);
foreach($words作为$word){
if(preg_match(“~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~”,$word))
回显“
”。$word; } //或 $res=preg_grep(“~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~”,$words); 印刷品(港币);;
为了测试字符串中是否存在字符,我使用
(?=[^s]*s)

[^s]*s
表示所有不是“s”的零次或多次,以及“s”的事物。
(?=…)
是一个先行断言,表示“后跟”。这只是一个检查,一个前瞻在匹配结果中不提供字符,但是这个特性的主要优点是可以多次检查相同的子字符串

你的模式有什么问题


/^(^s | ^m | ^e)/
将只提供以“s”或“m”或“e”开头的单词,因为
^
是一个锚点,表示“字符串的开头”。换句话说,您的模式与
/^([sme])/

相同,我想您得到的是
合成
,主要是因为它以
s
开头?但是正则表达式将匹配所有字符,而不仅仅是st。这不正确,如果单词以
s
开头,正则表达式将匹配,
m
e
我猜你得到的是
合成
主要是因为它以
s
开头?但是正则表达式将匹配所有字符,不仅仅是sts。这不正确,如果单词以
s
开头,正则表达式将匹配,
m
e
问题是为什么而不是如何:)卡西米尔:我用两个NOT@RishabhRaj:
^
仅在字符类中用作否定。问题是为什么而不是如何:)卡西米尔:我用的是两个^NOT@RishabhRaj:
^
仅在内部用作否定角色类。