用 perl 分割线
title: Football team: Real Madrid stadium: Santiago Bernabeu players: Zinédine Zidane, Ronaldo, Luís Figo, Roberto Carlos, Raúl personnel: José Mourinho (head coach) Aitor Karanka (assistant coach (es))
如何用 perl 来分割它:
title: Football
team: Real Madrid
stadium: Santiago Bernabeu
players: Zinédine Zidane Ronaldo Luís Figo Roberto Carlos Raúl
personnel: José Mourinho (head coach) Aitor Karanka (assistant coach (es))
title: Football team: Real Madrid stadium: Santiago Bernabeu players: Zinédine Zidane, Ronaldo, Luís Figo, Roberto Carlos, Raúl personnel: José Mourinho (head coach) Aitor Karanka (assistant coach (es))
How to split this with perl in:
title: Football
team: Real Madrid
stadium: Santiago Bernabeu
players: Zinédine Zidane Ronaldo Luís Figo Roberto Carlos Raúl
personnel: José Mourinho (head coach) Aitor Karanka (assistant coach (es))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用前瞻断言:
输出
Use a lookahead assertion:
Output
这应该可以做到。 line.txt 包含“标题:足球队:皇家马德里球场:圣地亚哥伯纳乌 球员:齐内丁·齐达内、罗纳尔多、路易斯·菲戈、罗伯托·卡洛斯、劳尔 人员:何塞·穆里尼奥(主教练)艾托尔·卡兰卡(助理教练(es))”
This should do it. line.txt contains "title: Football team: Real Madrid stadium: Santiago Bernabeu players: Zinédine Zidane, Ronaldo, Luís Figo, Roberto Carlos, Raúl personnel: José Mourinho (head coach) Aitor Karanka (assistant coach (es))"
与许多人在答案中所说的相反,您不需要前瞻(除了正则表达式自己的),您只需要捕获部分分隔符,如下所示:
我的完整解决方案如下:
转储看起来像这样:
Contrary to what many are saying in their answers, you do not need lookahead (other than the Regex's own), you would only need to capture part of the delimiter, like so:
My full solution below:
Dump looks like this:
最好的方法是使用带有零宽度先行的
split
命令:The best way is to use the
split
command using a zero-width lookahead: