Click here to Skip to main content
Click here to Skip to main content

Tagged as

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.

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:

    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;

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! Blush | :O

License

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

Share

About the Author

OriginalGriff
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?
Follow on   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalVolynsky Alex29-Mar-14 10:51 
QuestionNo need to write your own methods PinmemberJames Curran28-Mar-14 9:45 
AnswerRe: No need to write your own methods PinprotectorOriginalGriff28-Mar-14 10:18 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150123.1 | Last Updated 28 Mar 2014
Article Copyright 2014 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid