0

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

Tiffany Israel
  • 460
  • 1
  • 7
  • 22
  • what have you tried? are you familiar with any programming language? – codelessbugging Jun 13 '17 at 23:41
  • I'm familiar with HTML and PHP, I've dabble with Regex but nothing this complicated. I tried one regex thing that didn't work, but now I can't track it down. I've mostly been trying to do it inline with the multiple cursor. I was able to extrapolate how many numbers or in each line, but I realized I don't know how i can make that useful. – Tiffany Israel Jun 13 '17 at 23:50
  • regex and html aren't programming languages, so i think your best bet will be to use php. here are some resources toget you started: https://www.google.com.sg/amp/s/davidwalsh.name/basic-php-file-handling-create-open-read-write-append-close-delete/amp, https://stackoverflow.com/questions/13246597/how-to-read-a-file-line-by-line-in-php . The idea is to read the file line by line; for each line you explode (split) by hyphen to get user and the numbers, then split the numbers by comma. then you write these values to a new file – codelessbugging Jun 14 '17 at 00:00

2 Answers2

1

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
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • I figured out a different solution but hank you for your response NetMage however when I tried it, I got... #1234 - 1\n#1234 - 9 #5678 - 7,10\n#5678 - 23 – Tiffany Israel Jun 14 '17 at 15:00
  • I should add that I ran it twice and the other comma doesn't get replaced – Tiffany Israel Jun 14 '17 at 15:09
  • I lied, it wasn't working on an online regex that has been working better because of there being so many lines, but I tried it in sublime text and it works! You're a saint and a scholar, thank you! – Tiffany Israel Jun 14 '17 at 15:24
0

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
xiience
  • 427
  • 3
  • 15