简体   繁体   English

如何在C#正则表达式中找到字符串中所有附近的单词?

[英]how to find all near words in string in c# regex?

Regex.Matches("word1 word1 word1", @"\bword1\W+(?:\w+\W+){0,1}?word1\b");

this Regex return only one occurrence "word1 word1" 此正则表达式仅返回一个出现的"word1 word1"

What i need to add to this Regex to get all occurrences as below (may be some recursive pattern)? 我需要在此正则表达式中添加什么才能使所有出现的情况如下(可能是一些递归模式)?

  1. "word1 word1"
  2. "word1 word1"
  3. "word1 word1 word1"

Sorry my (confusing) example is not a question about repeated words. 抱歉,我的(令人困惑的)示例不是关于重复单词的问题。 To be more clear i give a second example : "xxx_w1 aa yyy_w1 zzz_w1". 为了更清楚,我举第二个例子:“ xxx_w1 aa yyy_w1 zzz_w1”。 I want to get the words ending by "_w1" separated by space ore one word (some distance) and the result for my example should be : 我想得到以“ _w1”结尾的单词,这些单词之间用空格隔开或一个单词(一定距离),本例的结果应为:

  1. xxx_w1 aa yyy_w1 xxx_w1 aa yyy_w1
  2. yyy_w1 zzz_w1 yyy_w1 zzz_w1

The issue with my Regex above is that i get only the first match instead of the two maches above. 我上面的正则表达式的问题是我只得到第一个比赛,而不是上面的两个比赛。

To find repeated words in a text, try this pattern: 要在文本中查找重复的单词,请尝试以下模式:

\b(\w+)\b(?:\s*\1)+

And here it is in action: 它在起作用:

查找字符串中的重复单词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM