简体   繁体   中英

Display all possible matches for a regex pattern

I have the following RegEx pattern in order to determine some 3-digit exchanges of phone numbers:

(?:2(?:04|[23]6|[48]9|50)|3(?:06|43|65)|4(?:03|1[68]|3[178]|50)|5(?:06|1[49]|79|8[17])|6(?:0[04]|13|39|47)|7(?:0[59]|78|8[02])|8(?:[06]7|19|73)|90[25])

It looks pretty daunting, but it only yields around 40 or 50 numbers. Is there a way in C# to generate all numbers that match this pattern? Offhand, I know I can loop through the numbers 001 thru 999, and check each number against the pattern, but is there a cleaner, built-in way to just generate a list or array of matches?

ie - {"204","226","236",...}

No, there is no off the shelf tool to determine all matches given a regex pattern. Brute force is the only way to test the pattern.

Update

It is unclear why you are using (?: ) which is the "Match but don't capture". It is used to anchor a match, for example take this phone text phone:303-867-5309 where we don't care about the phone: but we want the number.

The pattern used would be

(?:phone\:)(\d{3}-\d{3}-\d{4}) 

which would match the whole line, but the capture returned would just be the second match of the phone number 303-867-5309 .

So the (?: ) as mentioned is used to anchor a match capture at a specific point; with text match text thrown away.

With that said, I have redone your pattern with comments and a test to 2000:

string pattern = @"
^                            # Start at beginning of line so no mid number matches erroneously found
   (
       2(04|[23]6|49|[58]0)  # 2 series only match 204, 226, 236, 249, 250, 280
     |                       # Or it is not 2, then match:
       3(06|43|65)           # 3 series only match 306, 343, 365
    )
$                            # Further Anchor it to the end of the string to keep it to 3 numbers";

// RegexOptions.IgnorePatternWhitespace allows us to put the pattern over multiple lines and comment it. Does not
//     affect regex parsing/processing.

var results = Enumerable.Range(0, 2000) // Test to 2000 so we don't get any non 3 digit matches.
                        .Select(num => num.ToString().PadLeft(3, '0'))
                        .Where (num => Regex.IsMatch(num, pattern, RegexOptions.IgnorePatternWhitespace))
                        .ToArray();

Console.WriteLine ("These results found {0}", string.Join(", ", results));

// These results found 204, 226, 236, 249, 250, 280, 306, 343, 365

I took the advice of @LucasTrzesniewski and just looped through the possible values. Since I know I'm dealing w/ 3-digit numbers, I just looped through the numbers/strings “000” thru “999” and checked for matches like this:

private static void FindRegExMatches(string pattern)
{
    for (var i = 0; i < 1000; i++)
    {
        var numberString = i.ToString().PadLeft(3, '0');
        if (!Regex.IsMatch(numberString, pattern)) continue;

        Console.WriteLine("Found a match: {0}, numberString);
    }
}

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