需要为除 word1 或 word2 之外的任何单词找到正则表达式 [英] Need to find a regular expression for any word except word1 or word2

查看:22
本文介绍了需要为除 word1 或 word2 之外的任何单词找到正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我需要一个正则表达式,如果字符串是单词 (\w+) 则返回 true,除非它是单词 word1 或 word2.

Basically I need a regex which will return true if the string is a word (\w+) EXCEPT if it is the word word1 OR word2.

我尝试了很多东西,但不认为我很接近.帮助!

I've tried many things but dont think I'm even close. Help!

推荐答案

^(?!(?:word1|word2)$)\w+$

应该做你需要的.

(?!...) 是一个否定前瞻断言 确保不可能匹配当前位置的封闭表达式.

(?!...) is a negative lookahead assertion that ensures that it is not possible to match the enclosed expression at the current position.

这篇关于需要为除 word1 或 word2 之外的任何单词找到正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆