简体   繁体   中英

How to extract information from text validated against a regex

I wrote the following regex:

^(\[[0-9]+\])*$

It matches these exemplary texts:

  • ""
  • "[0]"
  • "[0][1][2][0][9]"

What I would like to do is to get a list of numbers stored within brackets. How to do this elegantly?

My approach:

public static IEnumerable<int> GetNumbers(string text)
{
    if (false == Regex.IsMatch(text, "^(\\[[0-9]+\\])*$"))
        throw new FormatException();

    return text
        .Trim('[', ']')
        .Split(new[] {"]["}, StringSplitOptions.None)
        .Select(int.Parse);
}

Solution:

public static IEnumerable<int> GetNumbers(string text)
{
    var match = Regex.Match(text, "^(\\[(?<number>[0-9]+)\\])*$");

    if (!match.Success)
        throw new FormatException();

    var captures = match.Groups["number"].Captures;
    foreach (Capture capture in captures)
        yield return int.Parse(capture.Value);
}

If you cannot change the pattern, but only code, you may use

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        var s1 = "[0][1][2][0][9]";
        var res1 = string.Join(",", GetNumbers(s1).ToList());         // Many
        Console.WriteLine(res1);
        Console.WriteLine(string.Join(",", GetNumbers("[8]").ToList())); // One
        Console.WriteLine(string.Join(",", GetNumbers("").ToList())); // Empty
    }
    public static IEnumerable<int> GetNumbers(string text)
    {
        if (string.IsNullOrWhiteSpace(text))
            return Enumerable.Empty<int>();                    // No text => return Empty
        var match = Regex.Match(text, @"^(\[[0-9]+\])*$");
        if (!match.Success)                                    // No match => return Empty
            return Enumerable.Empty<int>();
        return match.Groups[1].Captures
            .Cast<Capture>()                                   // Get the captures
            .Select(n => Int32.Parse(n.Value.Trim('[', ']'))); // Trim the brackets and parse
    }
}

See the IDEONE demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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