Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#
Tip/Trick

Using Linq with Regular Expression results

Rate me:
Please Sign up or sign in to vote.
4.85/5 (10 votes)
28 Mar 2014CPOL 28.9K   12   3
Want to use Linq, or Linq methods with the results from your Regex? Yes, so did I...annoying isn't it? So...a solution.

Introduction

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:

C#
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:

C#
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;

History

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

License

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


Written By
CEO
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?

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 PinPopular
James Curran28-Mar-14 8:45
James Curran28-Mar-14 8:45 
AnswerRe: No need to write your own methods Pin
OriginalGriff28-Mar-14 9:18
mveOriginalGriff28-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.