在包含行字开头的正则表达式拆分 [英] Regex Split at beginning of line containing word

查看:277
本文介绍了在包含行字开头的正则表达式拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个行包含特定字词每次文字分成段落。我已经设法分割在该单词的开头的文字,而不是在包含该字的行的开头。什么是合适的表情?



这是我所

 字符串[] =第Regex.Split(文字,@(= INT | EXT)?); 



我也不想失去阵列中的任何空段落。



这是输入

  INT。位置 -  DAY 
Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。

位置 - EXT。
Morbi的cursus格言tempor。在马萨非PORTA Phasellus马蒂斯。

地理位置INT。 - NIGHT

和我想它分裂保持相同的布局,但只是在段落

结果我已经是

  INT。位置 -  DAY 
Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。

位置 -

EXT。
Morbi的cursus格言tempor。在马萨非PORTA Phasellus马蒂斯。

地理位置

INT。 - 晚间

新段落的文字,而不是在该行启动



这是期望的结果。



第1

  INT。位置 -  DAY 
Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。

段落2



 位置 -  EXT。 
Morbi的cursus格言tempor。在马萨非PORTA Phasellus马蒂斯。

段落3



 位置INT。 -  NIGHT 



该段应始终包含单词INT行从头开始。或EXT。没有这个词。


解决方案

  Regex.Split(文字,(?= ^ 。?。?+ INT | ^ + EXT),RegexOptions.Multiline); 



选中该文本的情况。



 字符串文本=INT位置 -  DAY\\\
+
Lorem存有悲坐阿梅德,consectetur adipiscing elit.\\\
+
所在地 - EXT。 。\\\
+
Morbi的cursus格言tempor Phasellus马蒂斯在马萨非porta.\\\
+
LOCATION INT - NIGHT\\\


的String []解析度= Regex.Split(文字,(= ^ + INT | ^ + EXT)?。?。?,RegexOptions.Multiline);

的for(int i = 0; I< res.Count();我++)
{
INT LINENUMBER = I + 1;
Console.WriteLine(段落+ LINENUMBER +\\\
+ RES [I]);
}


#paragraph 1
#INT。位置 - DAY
#Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。

#paragraph 2
#LOCATION - EXT。
#Morbi的cursus格言tempor。在马萨非PORTA Phasellus马蒂斯。

#paragraph 3
#LOCATION INT。 - NIGHT


I'm trying to split a text into paragraphs each time a line contains a certain word. I already managed to split the text at the beginning of that word, but not at the beginning of the line containing that word. what's the right expression?

this is what I have

 string[] paragraphs = Regex.Split(text, @"(?=INT.|EXT.)");

I also want to lose any empty paragraphs in the array.

this is the input

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

and I want to split it up keeping the same layout but just in paragraphs.

The result I have is

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

The new paragraphs start at the word and not at the line.

This is the desired result

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

The paragraph should always start at the beginning of the line containing the word INT. or EXT. not at the word.

解决方案

Regex.Split(text, "(?=^.+?INT|^.+?EXT)", RegexOptions.Multiline);

check this text scenario

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

这篇关于在包含行字开头的正则表达式拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆