How do I match specified words in a sentence? If I have the sentence "lord of the rings" and want to remove "of" and "the" how do I write the regex?
如何匹配句子中的指定词?如果我有一个句子“指环王”,想要删除“of”和“the”,我该如何写regex?
I don't want it to match anything in for example "don't match them" or "don't match often". It should match when "the" or "of" is in the beginning or end of the row as well.
我不希望它匹配任何东西,例如“不匹配”或“不经常匹配”。当“或”在行的开头或结尾时,它也应该匹配。
The problem I encountered is when the words follow each other like in the example with "lord of the rings". When they share a space so to say.
我遇到的问题是,在“指环王”的例子中,这两个词彼此类似。当他们共享一个空间时。
The best I could manage is something like this:
我能做到的最好的事情是:
/^(the|of) | (the|of)$| (of|the) /gmi
But it doesn't solve all my problems.
但这并不能解决我所有的问题。
3 个解决方案
#1
1
This is a perfect task for word boundaries
对于单词边界来说,这是一项完美的任务
\b(of|the)\b
#2
1
You can use the lookahead group (?= )
[doc]:
您可以使用lookahead组(?=)[医生]:
/^(the|of) | (the|of)$| (of|the)(?= )/gmi
Demo : https://regex101.com/r/Bo4Ur1/1
演示:https://regex101.com/r/Bo4Ur1/1
#3
0
Would /(of\s.*the\s)/gmi
work? Or did I misunderstand what you're trying to do? This will find everything between the first 'of ' to the last 'the ' in your sentence.
/(\ s . * \ s)/ gmi公司工作吗?还是我误解你的意思了?这将会发现你句子中的第一个“of”和最后一个“the”之间的一切。
#1
1
This is a perfect task for word boundaries
对于单词边界来说,这是一项完美的任务
\b(of|the)\b
#2
1
You can use the lookahead group (?= )
[doc]:
您可以使用lookahead组(?=)[医生]:
/^(the|of) | (the|of)$| (of|the)(?= )/gmi
Demo : https://regex101.com/r/Bo4Ur1/1
演示:https://regex101.com/r/Bo4Ur1/1
#3
0
Would /(of\s.*the\s)/gmi
work? Or did I misunderstand what you're trying to do? This will find everything between the first 'of ' to the last 'the ' in your sentence.
/(\ s . * \ s)/ gmi公司工作吗?还是我误解你的意思了?这将会发现你句子中的第一个“of”和最后一个“the”之间的一切。