[英]Regex in PHP to match words with some excepetions
$matchstring = 'MS-DOS file';
$string1 = 'MS-DOS file';
$string2 = 'MS-DOS file, NE Windows';
$string3 = 'MS-DOS file, MZ OS-windows';
$string4 = 'MS-DOS file, Clear OS-windows';
I am writing the regex to march the string 'MS-DOS file' in above strings to only match $string1
and $string4
. 我正在编写正则表达式,以使上述字符串中的字符串“ MS-DOS文件”仅匹配$string1
和$string4
。 The pattern should not match the 'MS-DOS file' which is followed by the keywords 'NE' or 'MZ'. 该模式不应与“ MS-DOS文件”相匹配,后跟关键字“ NE”或“ MZ”。 For other it should match like it should match string 4 but not string 2,3 对于其他,它应该匹配,就像它应该匹配字符串4但不匹配字符串2,3
Any ideas ? 有任何想法吗 ?
I was tried this with my poor regex but no luck :( 我用可怜的正则表达式尝试了这个,但没有运气:(
if (preg_match("/MS-DOS file[\s]?[^MZ][^NE]/", $string1)){
echo "True";
} else{
echo "False";
}
Try this: 尝试这个:
"/MS-DOS file(?:,\s|$)(?!MZ|NE).*/"
You were overlooking the comma and misusing the character set square brackets. 您忽略了逗号,并滥用了字符集方括号。
EDIT: Also, I believe you want to check for 'NE', not 'NI'. 编辑:另外,我相信您想检查'NE',而不是'NI'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.