split() 但保留分隔符 [英] split() but keep delimiter

查看:44
本文介绍了split() 但保留分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过 Perl,我有一个基本问题.

I have never used Perl before and I have a basic question.

 my $string1 = "Hi. My name is Vlad. It is snowy outside.";

 my @array = split('.' $string1); ##essentially I want this, but I want the period to be kept

我想在'.'处分割这个字符串但我想保留这段时间.如何实现?

I want to split this string at the '.' But I want to keep the period. how can this be accomplished?

推荐答案

你可以使用lookbehind来做到这一点:

You can use lookbehind to do this:

split(/(?<=\.)/, $string)

正则表达式匹配句点后的空字符串.

The regex matches an empty string that follows a period.

如果想同时去掉句子之间的空格,可以改成:

If you want to remove the whitespace between the sentences at the same time, you can change it to:

split(/(?<=\.)\s*/, $string)

正面和负面的lookbehind在这里

Positive and negative lookbehind is explained here

这篇关于split() 但保留分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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