我想把一个短语分成更多的句子,用一个给定的词作为分隔符。输入短语示例:
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
分隔符,正则表达式将在第一行停止。
发布于 2017-02-22 04:25:47
只需做一个简单的分割而不是匹配。
my @text = split /[\r\n](?=You)/, $text;
这将在字符串You
之前存在的换行符或回车字符上进行拆分。
为了做一个干净的分割,最好添加+
,即.[\r\n]+
https://stackoverflow.com/questions/42391102
复制相似问题