I am trying to split using Regex.Split
strings like this one:
string criteria = "NAME='Eduard O' Brian' COURSE='Math II' TEACHER = 'Chris Young' SCHEDULE='3' CAMPUS='C-1' ";
We have the following 'reserved words': NAME, COURSE, TEACHER, SCHEDULE, CAMPUS.
It is required to split the original string into:
NAME='Eduard O' Brian'
COURSE='Math II'
TEACHER = 'Chris Young'
SCHEDULE='3'
CAMPUS='C-1'
The criteria for Split
is: to have the simple quote, followed by one or more spaces, followed by a 'reserved word'.
The closest expression I achieved is:
var match = Regex.Split(criteria, @"'[\s+]([NAME]|[COURSE]|[TEACHER]|[SCHEDULE]|[CAMPUS])", RegexOptions.CultureInvariant);
This is the complete source code:
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string criteria = "NAME='Eduard O' Brian' COURSE='Math II' TEACHER = 'Chris Young' SCHEDULE='3' CAMPUS='C-1' ";
var match = Regex.Split(criteria, @"'[\s+]([NAME]|[COURSE]|[TEACHER]|[SCHEDULE]|[CAMPUS])", RegexOptions.CultureInvariant);
foreach (var item in match)
Console.WriteLine(item.ToString());
Console.Read();
}
}
}
My code is doing this:
NAME='Eduard O' Brian' COURSE='Math II
T
EACHER = 'Chris Young
S
CHEDULE='3
C
AMPUS='C-1
It is deleting the last simple quote and is taking only the first letter of the reserved word. And COURSE in this sample has more than one space and is not working for it.
Thanks in advance!