split() but keep delimiter

perl, split

Solution

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)

Positive and negative lookbehind is explained here

Problem

``` 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?

Original source

Related problems