使用正则表达式从字符串中提取数字组

时间:2011-06-24 09:51:16

标签: c# .net regex

我需要转换一个像

这样的字符串
"[1,2,3,4][5,6,7,8]"

成整数组,调整为零而不是基于:

{0,1,2,3} {4,5,6,7}

以下规则也适用:

  • 该字符串必须至少包含一组带方括号的数字。
  • 每个组必须包含至少2个数字。
  • 每个数字必须是唯一的(不是我试图用正则表达式实现的)。
  • 0无效,但10,100等。

由于我不熟悉正则表达式,我现在正在使用两个;

@"^(?:\[(?:[1-9]+[\d]*,)+(?:[1-9]+[\d]*){1}\])+$";

@"\[(?:[1-9]+[\d]*,)+(?:[1-9]+[\d]*){1}\]";

我正在使用第一个检查输入,第二个使用方括号内的一组数字的所有匹配。

然后我使用.Net字符串操作来修剪方括号并提取数字,解析它们并减去1以得到我需要的结果。

我想知道我是否可以通过使用捕获来更好地获得数字,但不确定它们是如何工作的。


最终解决方案:

最后,我使用以下正则表达式来验证输入字符串

@"^(?<set>\[(?:[1-9]\d{0,7}(?:]|,(?=\d))){2,})+$"

agent-j的模式可以捕获所需的信息,但也匹配像“[1,2,3,4] [5]”这样的字符串,并且需要我对结果进行一些额外的过滤。

我通过命名组'set'访问捕获并使用第二个简单的正则表达式来提取数字。

'[1-9] \ d {0,7}'通过将数字限制为99,999,999并避免溢出异常来简化解析整数。

MatchCollection matches = new Regex(@"^(?<set>\[(?:[1-9]\d{0,7}(?:]|,(?=\d))){2,})+$").Matches(inputText);

if (matches.Count != 1)return;

CaptureCollection captures = matches[0].Groups["set"].Captures;

var resultJArray = new int[captures.Count][];
var numbersRegex =  new Regex(@"\d+");
for (int captureIndex = 0; captureIndex < captures.Count; captureIndex++)
{
    string capture = captures[captureIndex].Value;
    MatchCollection numberMatches = numbersRegex.Matches(capture);
    resultJArray [captureIndex] = new int[numberMatches.Count];
    for (int numberMatchIndex = 0; numberMatchIndex < numberMatches.Count; numberMatchIndex++)
    {
        string number = numberMatches[numberMatchIndex].Value;
        int numberAdjustedToZeroBase = Int32.Parse(number) - 1;
        resultJArray [captureIndex][numberMatchIndex] = numberAdjustedToZeroBase;
    }
}

3 个答案:

答案 0 :(得分:2)

string input = "[1,2,3,4][5,6,7,8][534,63433,73434,8343434]";
string pattern = @"\G(?:\[(?:(\d+)(?:,|(?=\]))){2,}\])";//\])+$";
MatchCollection matches = Regex.Matches (input, pattern);

首先,任何具有普通加重点的(regex)都是一个捕获组。这意味着正则表达式引擎将捕获(存储与该组匹配的位置)。为了避免这种情况(当你不需要它时,请使用(?:regex)。我在上面做了。

索引0是特殊的,它表示整个父级。 I.E. match.Groups [0] .Value始终与match.Value和match.Groups [0] .Captures [0] .Value相同。因此,您可以将Groups和Capture集合视为从索引1开始。

如下所示,每场比赛都包含一个括号内的数字组。您将希望使用每场比赛的第1组中的1-n捕获。

foreach (Match match in matches)
{
   // [1,2]
   // use captures 1-n from the first group.
   for (int i = 1; i < match.Group[1].Captures.Count; i++)
   {
      int number = int.Parse(match.Group[1].Captures[i]);
      if (number == 0)
         throw new Exception ("Cannot be 0.");
   }
}

Match[0] => [1,2,3,4]
  Group[0] => [1,2,3,4]
    Capture[0] => [1,2,3,4]
  Group[1] => 4
    Capture[0] => 1
    Capture[1] => 2
    Capture[2] => 3
    Capture[3] => 4
Match[1] => [5,6,7,8]
  Group[0] => [5,6,7,8]
    Capture[0] => [5,6,7,8]
  Group[1] => 8
    Capture[0] => 5
    Capture[1] => 6
    Capture[2] => 7
    Capture[3] => 8
Match[2] => [534,63433,73434,8343434]
  Group[0] => [534,63433,73434,8343434]
    Capture[0] => [534,63433,73434,8343434]
  Group[1] => 8343434
    Capture[0] => 534
    Capture[1] => 63433
    Capture[2] => 73434
    Capture[3] => 8343434

\G会导致匹配在最后一场比赛开始时开始(因此您将无法匹配[1,2] [3,4])。 {2,}满足您的要求,即每场比赛至少有2个号码。

即使存在0,表达式也会匹配。我建议您将该验证与其他非正则表达式一起使用。它将使正则表达式更简单。

答案 1 :(得分:1)

以下正则表达式将验证并吐出括号[]组的匹配组以及内部的每个数字

(?:([1-9][0-9]*)\,?){2,}



[1][5]  -  fail
[1]  -  fail
[]  -  fail
[a,b,c][5]  -  fail
[1,2,3,4]  -  pass
[1,2,3,4,5,6,7,8][5,6,7,8]  -  pass
[1,2,3,4][5,6,7,8][534,63433,73434,8343434]  -  pass

答案 2 :(得分:0)

\d+和全球旗帜怎么样?

相关问题