英文:
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]
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 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论