简体   繁体   English

C#RegEx.Split分隔符后跟特定的单词

[英]C# RegEx.Split delimiter followed by specific words

I am trying to split using Regex.Split strings like this one: 我试图使用像这样的Regex.Split字符串进行拆分:

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. 我们有以下'保留字': 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'. Split的标准是:使用简单引号,后跟一个或多个空格,后跟“保留字”。

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. 此示例中的COURSE有多个空格,并且无法正常工作。

Thanks in advance! 提前致谢!

You may simply split with 1+ whitespaces that are followed with your reserved words followed with = : 你可以简单地用1+个空格分开,然后用你的保留字跟着=

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

See the regex demo 请参阅正则表达式演示

Pattern details 图案细节

  • \\s+ - 1 or more whitespace chars \\s+ - 一个或多个空白字符
  • (?= - 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 (?:NAME|COURSE|TEACHER|SCHEDULE|CAMPUS) - 任何替代文字文本
    • \\s* - 0 or more whitespace chars (as there can be space(s) between reserved words and = ) \\s* - 0个或更多个空格字符(因为保留字和=之间可以有空格)
    • = - an equal sign = - 一个等号
  • ) - end of the lookahead. ) - 前瞻的结束。

C# demo : 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));

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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