Regex: add a new line for each sentence using vim

I was wondering how to turn a paragraph into bullet sentences in vim.

before:

sentence1. sentence2.  sentence3.  sentence4.  sentence5.  sentence6. 
sentence7. 

after

SENTENCE 1.

SENTENCE2.

sentence3

sentence4.

sentence5.

+5
source share
8 answers

Since all the other answers so far show how to do this, various programming languages, and you noted a question with Vim, here's how to do it in Vim:

:%s/\.\(\s\+\|$\)/.\r\r/g

I used two carriage returns according to the output format you indicated in the question. There are several alternative forms of regular expressions that you can use:

" Using a look-behind
:%s/\.\@<=\( \|$\)/\r\r/g
" Using 'very magic' to reduce the number of backslashes
:%s/\v\.( |$)/.\r\r/g
" Slightly different formation: this will also break if there
" are no spaces after the full-stop (period).
:%s/\.\s*$\?/.\r\r/g

and possibly many others.

A non-regexp way to do this:

:let s = getline('.')
:let lineparts = split(s, '\.\@<=\s*')
:call append('.', lineparts)
:delete

Cm:

:help pattern.txt
:help change.txt
:help \@<=
:help :substitute
:help getline()
:help append()
:help split()
:help :d
+8
source

/(?<=.) / , /.\n\n/. , , .

+1

Perl:

perl -e "$_ = <>; s/\.\s*/.\n/g; print"

, :

my $input = 'foo. bar. baz.';
$input =~ s/
    \.      # A literal '.'
    \s*     # Followed by 0 or more space characters
    /.\n/gx;       # g for all occurences, x to allow comments and whitespace in regex
print $input;

Python:

import re
input = 'foo. bar. baz.'
print re.sub(r'\.\s*', '.\n', input)
+1

/\.( |$)/g

, .

split . ( ) . (), .

+1

Ruby:

ruby-1.9.2 > a = "sentence1. sentence2.  sentence3. and array.split(). the end."
 => "sentence1. sentence2. sentence3. and array.split(). the end." 

ruby-1.9.2 > puts a.gsub(/\.(\s+|$)/, ".\n\n")                                
sentence1.

sentence2.

sentence3.

and array.split().

the end.

, ., (1 , ), . .

0

PHP:


<?php
$input = "sentence. sentence. sentence.";
$output = preg_replace("/(.*?)\\.[\\s]+/", "$1\n", $input);
?&gt

, - , . :


&lt?php
$input = "sentence. sentence. sentence.";
$arr = explode('.', $input);
foreach ($arr as $k => $v) $arr[$k] = trim($v);
$output = implode("\n", $arr);
?&gt

0

awk

$ awk '{$1=$1}1' OFS="\n" file
sentence1.
sentence2.
sentence3.
sentence4.
sentence5.
sentence6.
sentence7
0

, RegExr

(\ - =\S +)

-

\ \


RegExp: /(\-=?\s+)/g

pattern: (\-=?\s+)

flags: g

: 1

1: (\-=?\s+)

-

1 - 2 - 3 - 4 - 5-

1

2

3

4

5

0

All Articles