将短语拆分为以特定单词

时间:2017-02-22 12:10:38

标签: regex perl

我想将一个短语拆分成更多的句子,并将一个给定的单词定界。输入短语示例:

You must go right now.
She was walking quickly to the mall.
He should wait before going swimming.
Those girls are not trying very hard.
Ted might eat the cake.
You must go right now.
You can’t eat that!
My mother is fixing us some dinner.
Words were spoken.
These cards may be worth hundreds of dollars!
The teacher is writing a report.
You have woken up everyone in the neighborhood.

我期待得到的是:
1)

You must go right now.
She was walking quickly to the mall.
He should wait before going swimming.
Those girls are not trying very hard.
Ted might eat the cake.

2)

 You must go right now.

3)

You can’t eat that!

等等......

使用此代码我设法获得所有这些代码,但最后一个代码除外(因为在匹配的短语的最后没有You):

my $string = 'the phrase above';
my @results = ($string =~ /.+?(?=You)/g);

另外我注意到如果字符串有\n个分隔符,正则表达式将停在第一行。

1 个答案:

答案 0 :(得分:0)

只需进行简单的拆分而不是匹配。

my @text = split /[\r\n](?=You)/, $text;

这会拆分换行符或字符You之前存在的回车字符。

为了进行干净的拆分,最好添加+,即。 [\r\n]+