Parsing multivalued SharePoint fields with Regex
As Mark Arend points out on his blog, there are quite a number of ways SharePoint formats complex field values. His list even misses the Lookup multiple values variant that e.g. can be: "Kategori 2;#1;#Kategori 1".
To split the composite fields we need a little regular expression fission:
readonly static Regex _regex = new Regex(
@"([\#]?(\d+;\#)?)?(?'item'[^;]+)(\#?)",
RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.CultureInvariant);
static IEnumerable<string> SplitsAnySPMultiValueField(string value)
{
return _regex.Matches(value).Cast<Match>()
.Select(match => match.Groups["item"].Value);
}
Here is a list of examples.
Field Type | Input | Output |
Choices | ;First;Second;Third | {First,Second,Third} |
Lookup | 1;#First | {First} |
Lookup multiple | Kategori 2;#1;#Kategori 1 | {Kategori2,Kategori1} |
Person or Group | 42;#Zaphod Beeblebrox | {Zaphod Beeblebrox} |
The Hyperlink/Picture field value is comma separated, and I didn't want to split any text containing a comma, so that one is another story.