0

First string is

string str1 ="aaa sdf xxx fgd bbb efg rrr";

second string

string str2 ="ddd qwe ccc fgd eee ehj";

What should be the RegEx pattern ,Which say str1 and str2 are identical by pattern.

string pattern = "---------"; 

if(Regex.Match(str1 , pattern).Success== Regex.Match(str2 , pattern).Success)
{
     return true;
}

if string like this

string str1 = "afa fff fss fgd bfb efg rrr";

string str2 = "sdf qwe cfc fgd ege ehj";

the code should return false

4

3 回答 3

0

正则表达式将是:

[a-z\ ]*

在此处输入图像描述

于 2013-05-09T10:45:43.370 回答
0

尝试以下:

([a-zA-Z0-9])\1\1

参考:

http://icfun.blogspot.in/2008/07/regex-to-match-same-consecutive.html

希望它有帮助。

于 2013-05-09T10:45:57.633 回答
0
var segments1 = str1.Split(' ').Select((str, index) => new { Count = str.Length, Index = index });
var segments2 = str2.Split(' ').Select((str, index) => new { Count = str.Length, Index = index });
bool bIsSamePattern = true;
foreach(var segment in segments1)
{
  var segment2 = segments2.FirstOrDefault(x => x.Index == segment.Index);
  if (segment2 == null)
    break;
  bIsSamePattern = segment2.Count == segment.Count;
}

Regex如果这对您来说更容易,则无需使用。

于 2013-05-09T10:46:55.900 回答