REGEX使用关键字将字符串拆分为列表

我有以下字符串,我需要将字符串拆分为列表

string s = "Welcome to the Center

Batch #1 
English
Description about the course

Batch #2
Science
Description about the course

Batch #3
Social 
Description about the course "

我想使用"Batch"关键字拆分字符串,并且输出应如下所示。如何使用正则表达式进行拆分?

myList[0]="Batch #1 
English
Description about the course"

myList[1]="Batch #2
Science
Description about the course"

myList[2]="Batch #3 
Social
Description about the course"
hunanzlzzj 回答:REGEX使用关键字将字符串拆分为列表

您可以尝试以下操作:-

List<string> strList = Regex.Split(str,"Batch").Skip(1).ToList();
,

尝试一下。

您应该使用.*?

Batch.*?course

Batch.*?.\n.*\n.*

FIDDLE DEMO

enter image description here

,

您不需要正则表达式,只需将字符串拆分为两个连续的换行符(准确地说是\r\n\r\n序列):

  string s = @"Welcome to the Center

  Batch #1 
  English
  Description about the course

  Batch #2
  Science
  Description about the course

  Batch #3
  Social
  Description about the course ";

  var splitted = s.Split(new string[] { "\r\n\r\n" },StringSplitOptions.RemoveEmptyEntries).ToList();
  splitted.RemoveAt(0);
本文链接:https://www.f2er.com/3147129.html

大家都在问