【发布时间】:2016-03-03 21:22:08
【问题描述】:
我需要匹配文本并获取匹配的单词。
例如,我的文本是 HTML 格式,我将在下面用作示例
<p>Do not forget the error handling, I don't exactly know what happens if it wants to replace an occurence and can't find it</p>
<p>Edit: If you have multiple entries which should be replaced, loop the replace part until it will not be able to replace anymore then it will throw an error you can catch to continue</p>
匹配案例:
案例1(如果中间有匹配词):occurence
结果:I don't exactly know what happens if it wants to replace an occurence and can't find it
案例2(如果第一个单词中的单词匹配):Do not
结果:Do not forget the error handling, I don't exactly know what happens if it wants to replace an occurence and can't find it
案例3(如果匹配文本中最后一个单词):to continue
结果:If you have multiple entries which should be replaced, loop the replace part until it will not be able to replace anymore then it will throw an error you can catch to continue
如果它是文本之间的单词,它应该在单词周围显示文本。 如果匹配词是第一个词,那么它应该从第一个词本身获取文本
如果匹配是最后一个单词,则从匹配的最后一个单词之前获取文本。
正则表达式(?<=(\w+)\s)?(continue)(?=\s(\w+))?
它只匹配单词我怎样才能让我们在匹配的关键字周围说 10 -15 个单词。
这可以使用正则表达式吗
【问题讨论】:
-
您的问题不清楚。请重新考虑文本案例。怎么可能在一个测试用例中,关键字前面恰好需要 12 次重复,而在另一种测试用例中,您需要超过 15 次?