简体   繁体   中英

C# RegEx.Split delimiter followed by specific words

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!

You may simply split with 1+ whitespaces that are followed with your reserved words followed with = :

var results = Regex.Split(s, @"\s+(?=(?:NAME|COURSE|TEACHER|SCHEDULE|CAMPUS)\s*=)");

See the regex demo

Pattern details

  • \\s+ - 1 or more whitespace chars
  • (?= - start of a positive lookahead that, immediately to the right of the current location, requires the following text:
    • (?:NAME|COURSE|TEACHER|SCHEDULE|CAMPUS) - any of the alternative literal texts
    • \\s* - 0 or more whitespace chars (as there can be space(s) between reserved words and = )
    • = - an equal sign
  • ) - end of the lookahead.

C# demo :

var 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)\s*=)");
Console.WriteLine(string.Join("\n", match));

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM