正则表达式,用于查找不匹配特定大小写的特定单词?

huangapple go评论56阅读模式
英文:

Regex that finds a specific word that doesn't match a specific upper/lowercase?

问题

是不是可能有一个正则表达式,匹配一个特定的单词,而不区分大小写?

例如:FooBar - 我想查找这个单词,只要它不包含大写的F和B,其他都是小写。

foobar - 匹配
FooBar - 不匹配
aWord  - 不匹配
fOoBaR - 匹配
英文:

Is it possible to have a regular expression that matches a specific word that doesn't match the specific upper/lowercase?

For example: FooBar - I'd like to look for this word only if it doesn't have a capital F and B and the rest lowercase.

foobar - match
FooBar - not match
aWord  - not match
fOoBaR - match

答案1

得分: 1

此正则表达式:

^f[oO]{2}[bB][Aa][rR]

在线演示

该正则表达式匹配如下:

节点 解释
^ 字符串的开头
f f
[oO]{2} 任何字符:'o','O'(2次)
[bB] 任何字符:'b','B'
[Aa] 任何字符:'A','a'
[rR] 任何字符:'r','R'
英文:

This regex:

^f[oO]{2}[bB][Aa][rR]

online demo

The regular expression matches as follows:

Node Explanation
^ the beginning of the string
f f
[oO]{2} any character of: 'o', 'O' (2 times)
[bB] any character of: 'b', 'B'
[Aa] any character of: 'A', 'a'
[rR] any character of: 'r', 'R'

答案2

得分: 1

匹配模式中的正则表达式会匹配包含"Foobar"(不区分大小写)的单词。以下是每个单词是否匹配的结果:

  • foobar: 是
  • FooBar: 否
  • Foobar: 是
  • aWord: 否
  • fOoBaR: 是
英文:

Another option which would also match Foobar which I understand would be a correct match, since only one letter F is capital and not both (B and F), correct?

(?!FooBar)([fF][oO]{2}[bB][aA][rR])
Word Match
foobar yes
FooBar no
Foobar yes
aWord no
fOoBaR yes

huangapple
  • 本文由 发表于 2023年3月7日 06:45:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定