Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

regex - Inserting a space between a dot and a character using simple search/replace with regular expressions

I want to separate sentences by inserting a space between each period and letter but not between anything else like a dot and a bracket or a dot 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 what I'm interested in is can it be done in a text editor that supports search/replace based on regexes? The problem is that it would match both the dot and the character and replace will completely obliterate both. In other words, is there a way to match "nothing" between those two characters?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use back references in the replace string. Typically it would look something like:

Search regex:

(.)(w)

Replacement pattern (notice the space):

$1 $2

The back references are stand-ins for the corresponding groups.

Alternatively, you could use lookarounds:

(?<=.)(?=w)

This doesn't "capture" the text, it would only match the position between the period and the letter/number (a zero-length string). Replacing it would, essentially, insert some text.

Really, though, it depends on the capabilities of your text editor. Very few text editors have a "complete" regular expression engine built-in. I use TextPad which has its own flavor of regular expression which largely does not support lookarounds (forcing me to use the first approach).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...