使用Rexex找到相同的三个单词 [英] find Same three word using Rexex

查看:99
本文介绍了使用Rexex找到相同的三个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想在一个句子中找到相同的三个字

例如:这是适合所有人的好时候



我想找到好和换



我用过

Hi I would like to find same three word in a sentence
Eg: This is good good good time for for for all

I would like to find "good" and "for"

I used

\b(\w+)\s+\1\b





但是没有得到结果



but didn't get the result

推荐答案

试试这个:

Try this:
\b(\w+)\b\s+\1\b\s+\1\b



\ 1表示组1有效地引用(\w +)

,整个正则表达式找到重复三次的单词。


\1 means group 1 which refers to the (\w+)
effectively, the whole regex finds word that repeats thrice.




查看这些链接并深入了解正则表达式



1. 30分钟正则表达式教程 [ ^ ]



2. https://msdn.microsoft.com/en-us/library/ ms228595.aspx [ ^ ]



3. http://www.dotnetperls.com/regex -match [ ^ ]
Hi,
See these links and dig more about regular expression

1. The 30 Minute Regex Tutorial[^]

2. https://msdn.microsoft.com/en-us/library/ms228595.aspx[^]

3. http://www.dotnetperls.com/regex-match[^]


这不是Regex擅长的东西:它是文本处理器,而不是语法分析器。它不擅长比较值以确保它们是相同的。



相反,在C#中使用string.Split将句子分成单个单词,然后要么用循环处理它们,要么用Linq处理它们 - 它会工作很多,更好,以后更容易阅读和修改。
That isn't really something a Regex is good at: it's a text processor, not a syntax analyser. It isn't good at comparing values to make sure they are the same.

Instead, do it in C# by using string.Split to break the sentence into individual words, and then either process them with a loop, or with Linq - it'll work a lot, lot better and be easier to read and modify later.


这篇关于使用Rexex找到相同的三个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆