正则表达式 - 查找所有不以特定前缀开头的匹配词 [英] Regex - Find all matching words that that don't begin with a specific prefix
问题描述
如何构造正则表达式来查找以字符串结尾但不以字符串开头的所有单词?
How would I construct a regular expression to find all words that end in a string but don't begin with a string?
例如找出以下句子中所有以 'friend' 结尾且不以单词 'girl' 开头的单词:
e.g. Find all words that end in 'friend' that don't start with the word 'girl' in the following sentence:
当男朋友和女朋友要求与他们交朋友时,他们获得了一个朋友"
"A boyfriend and girlfriend gained a friend when they asked to befriend them"
粗体的项目应该匹配.'女朋友'这个词不应该.
The items in bold should match. The word 'girlfriend' should not.
推荐答案
在我的脑海里,你可以尝试:
Off the top of my head, you could try:
\b # word boundary - matches start of word
(?!girl) # negative lookahead for literal 'girl'
\w* # zero or more letters, numbers, or underscores
friend # literal 'friend'
\b # word boundary - matches end of word
更新
这是另一种非显而易见的方法,它应该适用于正则表达式的任何现代实现:
Here's another non-obvious approach which should work in any modern implementation of regular expressions:
假设您希望提取出现在多个上下文中的模式,但您只想匹配出现在特定上下文中的模式,您可以使用更改,首先指定您不想要的内容,然后捕获您所做的.
Assuming you wish to extract a pattern which appears within multiple contexts but you only want to match if it appears in a specific context, you can use an alteration where you first specify what you don't want and then capture what you do.
因此,使用您的示例,要提取除 girlfriend
之外的所有以 friend
结尾或以 friend
结尾的单词,您可以使用:
So, using your example, to extract all of the words that either are or end in friend
except girlfriend
, you'd use:
\b # word boundary
(?: # start of non-capture group
girlfriend # literal (note 1)
| # alternation
( # start of capture group #1 (note 2)
\w* # zero or more word chars [a-zA-Z_]
friend # literal
) # end of capture group #1
) # end of non-capture group
\b
注意事项:
- 这是我们不捕捉的.
- 这就是我们做要捕捉的内容.
- This is what we do not which to capture.
- And this is what we do which to capture.
可以描述为:
- 对于所有单词
- 首先,匹配'girlfriend'并且不捕获(丢弃)
- 然后匹配以friend"或以friend"结尾的任何单词并捕获它
在 JavaScript 中:
In Javascript:
const target = 'A boyfriend and girlfriend gained a friend when they asked to befriend them';
const pattern = /\b(?:girlfriend|(\w*friend))\b/g;
let result = [];
let arr;
while((arr=pattern.exec(target)) !== null){
if(arr[1]) {
result.push(arr[1]);
}
}
console.log(result);
运行时将打印:
[ 'boyfriend', 'friend', 'befriend' ]
这篇关于正则表达式 - 查找所有不以特定前缀开头的匹配词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!