Using Regular Expressions To Build SPList Collections

One of my friends just asked me a rather distinctive question. What if I wanted to build a typed collection of SPList objects by passing in a SharePoint web (SPWeb) and a pattern to match the list by (string) into some arbitrary method?

In order to do this, we can presume since the pattern is being passed in as a parameter that in some additional scene it is being constructed and is well-formed. Therefore, we can do a uncomplicated regular expression match leveraging the list title and the pattern. Following, test the result of the match, ensuring that it coordinates to success. We then return the collection if the count is higher than 0, otherwise just return null.

public static List PoolListsByExpression(string pattern, SPWeb web)
List tempListCollection = new List();
foreach (SPList list in web.Lists)
if (Regex.Match(list.Title, pattern).Success)
if (tempListCollection.Count > 0)
return tempListCollection;
return null;


No Comments


  1. Links (7/15/2008) « Steve Pietrek - Everything SharePoint - [...] Using Regular Expressions To Build SPList Collections [...]
  2. Websites tagged "patternmatch" on Postsaver - [...] 2008-10-08 - Using Regular Expressions To Build SPList Collections saved by ajonser2008-10-03 - Coin slots saved by sad12672008-10-02 -…

Leave a Reply

Your email address will not be published. Required fields are marked *