首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >将短语分成以特定单词开头的句子

将短语分成以特定单词开头的句子
EN

Stack Overflow用户
提问于 2017-02-22 12:10:37
回答 1查看 57关注 0票数 0

我想把一个短语分成更多的句子,用一个给定的词作为分隔符。输入短语示例:

代码语言:javascript
代码运行次数:0
运行
复制
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)

代码语言:javascript
代码运行次数:0
运行
复制
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)

代码语言:javascript
代码运行次数:0
运行
复制
 You must go right now.

3)

代码语言:javascript
代码运行次数:0
运行
复制
You can’t eat that!

等等..。

使用这段代码,我成功地获得了所有代码,除了最后一个代码(因为在要匹配的短语的最后一个位置没有You ):

代码语言:javascript
代码运行次数:0
运行
复制
my $string = 'the phrase above';
my @results = ($string =~ /.+?(?=You)/g);

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-22 12:25:47

只需做一个简单的分割而不是匹配。

代码语言:javascript
代码运行次数:0
运行
复制
my @text = split /[\r\n](?=You)/, $text;

这将在字符串You之前存在的换行符或回车字符上进行拆分。

为了做一个干净的分割,最好添加+,即.[\r\n]+

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42391102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档