Insert a space between a period and a character using a simple search / replace using regular expressions

I want to separate sentences by inserting a space between each period and a letter, but not between anything else, like a period and a bracket or a period and a comma.

Consider this:

This is a text.With some dots.Between words.(how lovely). 

This probably has some solution in Perl or PHP, but I'm wondering if it can be done in a text editor that supports regular expression search / replace? The problem is that it will coincide with both the point and the character, and the replacement will completely destroy both. In other words, is there a way to match "nothing" between these two characters?

+8
regex replace
source share
4 answers

You can use backlinks in the replace line. This usually looks something like this:

Search for regex:

 (\.)(\w) 

Sample replacement (note the space):

 $1 $2 

Backlinks are stand-ins for related groups.

Alternatively, you can use search queries:

 (?<=\.)(?=\w) 

This does not “capture” the text, it will only match the position between the period and the letter / number (zero-length string). Replacing it essentially inserts some text.

Indeed, it depends on the capabilities of your text editor. Very few text editors have a built-in regex engine. I use TextPad, which has its own regex flavor that pretty much doesn't support search queries (forcing me to use the first approach).

+11
source share

The language is not specified, and I used PHP, but the expression is quite general and can be reused in other environments:

 <?php $s = 'This is a text.With some dots.Between words.(how lovely).'; $r = '~(\w)(\.)(\w)~'; echo preg_replace($r, '$1 $3', $s); 

this code leads to the following line output:

 This is a text With some dots Between words.(how lovely). 
  • (\ w) matches exactly one alphanumeric character before the dot
  • (.) matches the dots
  • (\ w) matches exactly one alphanumeric character after the period

the first and third matches are referred to in the replacement string as $ 1 and $ 3

+1
source share

This code segment solves your problem:

 preg_replace('/([a-zA-Z]{1})\.([a-zA-Z]{1})/', '$1. $2', 'This is a text.With some dots.Between words.(how lovely).'); 

You must detect any character before and after the point and replace it with blanco.

+1
source share

In Perl:

 $msg =~ s/\.([a-zA-Z])/\. \1/g 

In vim (whole file):

 :%s/\.([a-zA-Z])/\. \1/g 

In Visual Studio, it will be

 \.([a-zA-Z]) 

in the Find That: section and

 \. \1 

in "Replace with:".

In general, most editors that support regex search usually have capture groups that allow you to store part of the expressed match and use it in replaceable text. In the expressions above, everything in () “captured”, and I include it with \1 .

0
source share

All Articles