我有以下PHP正则表达式匹配以字母'Z'开头的单词。
/^Z/
我怎样才能将此正则表达式扩展为仅匹配以“Z”开头但没有“h”字母的单词。
我的代码是
if(preg_match('/^Z/','Zoe, Zoey, Zoho, Zander, Zap',$matches)){
echo $matches[0];
}
答案 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