简体   繁体   English

正则表达式排除特定单词

[英]RegEx to exclude particular words

I am working on RegEx that will match a string that contains only uppercase letters and digits but not exact words ABC or XYZ or LMN.我正在研究正则表达式,它将匹配仅包含大写字母和数字但不包含精确单词 ABC 或 XYZ 或 LMN 的字符串。

^(?!ABC|XYZ|LMN)([A-Z0-9]+)$

This works for most of the cases but fails for input ABC12, XYZ8 etc.这适用于大多数情况,但输入 ABC12、XYZ8 等失败。

How can I improve this to match the requirement.我怎样才能改进它以符合要求。

I've referred RegEx to exclude a specific string constant but couldn't tackle this one.我已经提到RegEx 来排除特定的字符串常量,但无法解决这个问题。

If you are trying to match the whole string or line using ^ and $, then you can put the $ inside your negative lookahead.如果您尝试使用 ^ 和 $ 匹配整个字符串或行,那么您可以将 $ 放在负前瞻中。 This will allow ABC12 to match.这将允许 ABC12 匹配。

^(?!(ABC|XYZ|LMN)$)([A-Z0-9]+)$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何排除句子中的某些单词。 正则表达式? - How to exclude certain words in sentence. regex? 正则表达式为大写所有单词,但不包括 '(撇号) - Regex to Upper Case all words but exclude ' (apostrophe) 正则表达式排除以井号标签开头的单词 - Regex to exclude words beginning with hash tag 从正则表达式匹配中排除单词数组 - Exclude array of words from regex match in Javascript 正则表达式以匹配段落中的单词,但排除内部HTML标记 - Regex to match words in a paragraph but exclude inner HTML tags 如何使用REGEX中的或选项提取和捕获特定的多个单词? - How to extract and capture particular multiple words using or option in REGEX.? 正则表达式匹配除特定标志格式的单词之外的所有内容 - Regex Match everything except words of particular flag format 正则表达式匹配特定单词组之后由一个点分隔的单词,直到空格 - Regex to match words separated by one dot after particular set of words until whitespace JavaScript Regex当字母在某些特殊单词的开头/结尾/之间时,如何排除字母替换? - JavaScript Regex How to exclude letter replacement when the letter is start/end/between of some special words? 正则表达式排除具有特定属性的标签详细信息,并仅选择特定标签 - regex to exclude the tag details which having a particular attribute and select only specific tags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM