正则表达式在包含单词的行的开头拆分
Regex Split at beginning of line containing word
每次一行包含特定单词时,我都会尝试将文本拆分为多个段落。我已经设法在该词的开头拆分文本,但不是在包含该词的行的开头。正确的表达方式是什么?
这就是我的
string[] paragraphs = Regex.Split(text, @"(?=INT.|EXT.)");
我也想丢失数组中的任何空段落。
这是输入
INT. LOCATION - DAY
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
LOCATION - EXT.
Morbi cursus dictum tempor. Phasellus mattis at massa non porta.
LOCATION INT. - NIGHT
我想将其拆分,保持相同的布局,但只是分段。
我得到的结果是
INT. LOCATION - DAY
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
LOCATION -
EXT.
Morbi cursus dictum tempor. Phasellus mattis at massa non porta.
LOCATION
INT. - NIGHT
新段落从单词而不是行开始。
这是想要的结果
第 1 段
INT. LOCATION - DAY
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
第 2 段
LOCATION - EXT.
Morbi cursus dictum tempor. Phasellus mattis at massa non porta.
第 3 段
LOCATION INT. - NIGHT
该段落应始终从包含单词 INT 的行的开头开始。或分机。不是这个词。
Regex.Split(text, "(?=^.+?INT|^.+?EXT)", RegexOptions.Multiline);
检查这个文本场景
string text = "INT. LOCATION - DAY\n" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" +
"LOCATION - EXT.\n" +
"Morbi cursus dictum tempor. Phasellus mattis at massa non porta.\n" +
"LOCATION INT. - NIGHT\n";
string[] res = Regex.Split(text, "(?=^.+?INT|^.+?EXT)", RegexOptions.Multiline);
for (int i = 0; i < res.Count(); i++)
{
int lineNumber = i + 1;
Console.WriteLine("paragraph " + lineNumber + "\n" + res[i]);
}
#paragraph 1
#INT. LOCATION - DAY
#Lorem ipsum dolor sit amet, consectetur adipiscing elit.
#paragraph 2
#LOCATION - EXT.
#Morbi cursus dictum tempor. Phasellus mattis at massa non porta.
#paragraph 3
#LOCATION INT. - NIGHT
每次一行包含特定单词时,我都会尝试将文本拆分为多个段落。我已经设法在该词的开头拆分文本,但不是在包含该词的行的开头。正确的表达方式是什么?
这就是我的
string[] paragraphs = Regex.Split(text, @"(?=INT.|EXT.)");
我也想丢失数组中的任何空段落。
这是输入
INT. LOCATION - DAY
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
LOCATION - EXT.
Morbi cursus dictum tempor. Phasellus mattis at massa non porta.
LOCATION INT. - NIGHT
我想将其拆分,保持相同的布局,但只是分段。
我得到的结果是
INT. LOCATION - DAY
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
LOCATION -
EXT.
Morbi cursus dictum tempor. Phasellus mattis at massa non porta.
LOCATION
INT. - NIGHT
新段落从单词而不是行开始。
这是想要的结果
第 1 段
INT. LOCATION - DAY
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
第 2 段
LOCATION - EXT.
Morbi cursus dictum tempor. Phasellus mattis at massa non porta.
第 3 段
LOCATION INT. - NIGHT
该段落应始终从包含单词 INT 的行的开头开始。或分机。不是这个词。
Regex.Split(text, "(?=^.+?INT|^.+?EXT)", RegexOptions.Multiline);
检查这个文本场景
string text = "INT. LOCATION - DAY\n" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" +
"LOCATION - EXT.\n" +
"Morbi cursus dictum tempor. Phasellus mattis at massa non porta.\n" +
"LOCATION INT. - NIGHT\n";
string[] res = Regex.Split(text, "(?=^.+?INT|^.+?EXT)", RegexOptions.Multiline);
for (int i = 0; i < res.Count(); i++)
{
int lineNumber = i + 1;
Console.WriteLine("paragraph " + lineNumber + "\n" + res[i]);
}
#paragraph 1
#INT. LOCATION - DAY
#Lorem ipsum dolor sit amet, consectetur adipiscing elit.
#paragraph 2
#LOCATION - EXT.
#Morbi cursus dictum tempor. Phasellus mattis at massa non porta.
#paragraph 3
#LOCATION INT. - NIGHT