Tiffany Israel
Tiffany Israel

Reputation: 470

Replace each instance of a character in a string with the first number of the string in Sublime Text

This is probably a very obscure reqeust but I have 6,000+ lines that look like this..

#1234 - 1,9
#5678 - 7,10,23

I need to take the #1234 (user id) and pair it with each number in the string to make new strings, i.e.

#1234 - 1
#1234 - 9
#5678 - 7
#5678 - 10
#5678 - 23

How can this be done? The lines all vary in how many numbers/commas are in the strings

Upvotes: 0

Views: 158

Answers (2)

NetMage
NetMage

Reputation: 26907

To user regex replace, you can repeat this search and replace until no more remain:

Search for:

^((#[0-9]+ - )[0-9,]+),([0-9]+)$

and replace with

$1\n$2$3

Upvotes: 1

xiience
xiience

Reputation: 457

If python is an option, for this specific case this code should work:

from re import findall

input = """#1234 - 1,9
#5678 - 7,10,23"""
regexStr = "\d+"
for line in input.split('\n'):
    matches = findall(regexStr, line)
    for i in range(1, len(matches)):
        print("#" + matches[0] + " - " + matches[i])

output:

#1234 - 1
#1234 - 9
#5678 - 7
#5678 - 10
#5678 - 23

Upvotes: 0

Related Questions