我正在寻找一个搜索文件并且不返回(即它排除)在字符串中连续重复 3 次或更多次的字符的正则表达式。我在下面尝试过这个表达式,但它没有做这项工作:( ..看起来向前和向后的东西,不包括具有3个或更多重复背靠背字符的字符串。即它应该返回abcdefg
,但不是3333ahg
or gagjjjjagy
orhdajgjga111
(?!(.)\1{3})
尝试使用以下正则表达式匹配包含 3 个或更多重复的背靠背字符的字符串
(.)\1{2,}
然后使用标志反转匹配。大多数语言都支持它。
例如,与grep
$ cat file
abcdefg
gagjagyyy
3333ahg
$ grep -v -E '(.)\1{2,}' file
abcdefg
如果你使用 C#,你可以试试这个:
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
class Program
{
const string
isMatch = "IsMatch",
pattern = @"(?:(?<Open>\w*?(\w)\1{{2,}}\w*)|(?<{0}>\w*))";
static void Main(string[] args)
{
var input = File.ReadAllText("input.txt");
var regex = String.Format(pattern, isMatch);
var matches = Regex.Matches(input, regex)
.Cast<Match>()
.Select<Match, Group>(m => m.Groups[isMatch])
.Where(g => g.Value != string.Empty)
.ToList();
matches.ForEach(m => Console.WriteLine(m.Value));
}
}
尝试这个:
^(?!.*(.)\1\1.*$).+$ - matches whole string as one word
(?=\b|^)(?!\w*(\w)\1\1\w*)\w+(?:\b|$) - matches one word