PHP-Regular表达式仅匹配字符串中没有特定字符的单词

时间:2013-11-27 19:27:28

标签: php regex

我有以下PHP正则表达式匹配以字母'Z'开头的单词。

/^Z/

我怎样才能将此正则表达式扩展为仅匹配以“Z”开头但没有“h”字母的单词。

我的代码是

if(preg_match('/^Z/','Zoe, Zoey, Zoho, Zander, Zap',$matches)){
    echo $matches[0];
}

3 个答案:

答案 0 :(得分:0)

  

How could I extend this regex to only match words starting with 'Z', but without a 'h' letter.

您可以使用:

'/^Z[^h\W]*\b/'

答案 1 :(得分:0)

使用[^h]对象匹配除h之外的任何内容,\W以排除非字字符,以及\b字边界。

^Z[^h\W]+\b

答案 2 :(得分:0)

使用正则表达式\bZ[a-gi-z]*\b

相关问题