Click here to Skip to main content
12,892,810 members (54,877 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 28 Mar 2014

Using Linq with Regular Expression results

, 28 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Want to use Linq, or Linq methods with the results from your Regex? Yes, so did I...annoying isn't it? So...a solution.


I like regexes. I'm not that fond of the syntax, but it's a complex task and the language is understandably complex - and Expresso[^] makes it a lot easier anyway - it's free, and it examines and generates Regular expressions.

But...I also like Linq and Linq methods and the deferred execution they allow. Unfortunately, the two are not compatible: the MatchCollection class does not implement IEnumerable<T>, so you can't use Linq.

So I had a little think, and realised there was nothing stopping me from converting a MatchCollection to an IEnumerable<Match> - it's actually really, really simple: all you need is yield return

The methods

I defined two methods: one for the MatchCollection to Enumerate the Matches, the other for the Match to Enumerate the Groups, and made them simple extension methods:

public static class ExtensionMethods
    /// <summary>
    /// Returns the input typed as a generic IEnumerable of the groups
    /// </summary>
    /// <param name="m"></param>
    /// <returns></returns>
    public static IEnumerable<System.Text.RegularExpressions.Group> AsEnumerable(this System.Text.RegularExpressions.GroupCollection gc)
        foreach (System.Text.RegularExpressions.Group g in gc)
            yield return g;
    /// <summary>
    /// Returns the input typed as a generic IEnumerable of the matches
    /// </summary>
    /// <param name="mc"></param>
    /// <returns></returns>
    public static IEnumerable<System.Text.RegularExpressions.Match> AsEnumerable(this System.Text.RegularExpressions.MatchCollection mc)
        foreach (System.Text.RegularExpressions.Match m in mc)
            yield return m;
And that's all you need...

Using the code

Just include the above class anywhere in your namespace, (or in a class library file, with the appropriate using statement) and off you go:

string input = "<Polygon>\n" +
               "          <Vertex x=\"9352.7606\" y=\"8250.6001\" z=\"505.3871\" />\n" +
               "          <Vertex x=\"9352.7573\" y=\"8250.6001\" z=\"505.3844\" />\n" +
               "          </Polygon>";
MatchCollection matches = Regex.Matches(input, @"(\d+)(\.\d+)?");
var x = matches.AsEnumerable().Select(m => m.Value);
var y = from match in matches.AsEnumerable()
        select match.Value;


2014 Mar 28 First version.

2014 Mar 28 Removed unnecessary usage example.

2014 Mar28 Converted references in the text from IEnumerable to IEnumerable<T> - Thanks ProgramFOX! :O


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Volynsky Alex29-Mar-14 9:51
professionalVolynsky Alex29-Mar-14 9:51 
QuestionNo need to write your own methods Pin
James Curran28-Mar-14 8:45
memberJames Curran28-Mar-14 8:45 
AnswerRe: No need to write your own methods Pin
OriginalGriff28-Mar-14 9:18
protectorOriginalGriff28-Mar-14 9:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 28 Mar 2014
Article Copyright 2014 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid