Find & Replace digit by digit and space in Sublime Text
Asked Answered
K

3

5

I have lot of digits and I want to add spaces in between them, like so:

0123456789 --> 0 1 2 3 4 5 6 7 8 9

I'm trying to do this using the search and replace function in Sublime Text. What I've tried so far is using \S to find all characters (there are only digits so it doesn't matter) and \1\s to replace them. However, this deletes the digits and replaces them with s. Does anybody know how to do this?

Kenton answered 22/6, 2014 at 19:42 Comment(0)
H
7

You can use a combination of Lookahead and Lookbehind assertions to do this. Use Ctrl + H to open the Search and Replace, enable Regular Expression, input the following and click Replace All

Find What: (?<=\d)(?=\d)
Replace With: empty space

Live Demo


Explanation:

(?<=        # look behind to see if there is:
  \d        #   digits (0-9)
)           # end of look-behind
(?=         # look ahead to see if there is:
  \d        #   digits (0-9)
)           # end of look-ahead
Harl answered 22/6, 2014 at 20:0 Comment(0)
H
4

Press Ctrl + H to open the Search and Replace dialog (make sure you click the .* at the left to enable regular expression mode):

  • Search for: (\d)(?!$)
  • Replace with: $1[space]

The regular expression (\d)(?!$) matches all the digits except the one at the end of the line.

Here's how it looks like:

enter image description here

Hunsaker answered 22/6, 2014 at 19:47 Comment(0)
R
1

you could use this pattern (\d)(?=\d) and replace with $1 <- $1{space}
Demo

Regine answered 22/6, 2014 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.