eng2019
eng2019

Reputation: 1035

Regex: Split characters with "/"

I have these strings, for example: ['2300LO/LCE','2302KO/KCE']

I want to have output like this: ['2300LO','2300LCE','2302KO','2302KCE']

How can I do it with Regex in Python?

Thanks!

Upvotes: 2

Views: 95

Answers (3)

mrid
mrid

Reputation: 5796

You can try something like this:

list1 = ['2300LO/LCE','2302KO/KCE']
list2 = []

for x in list1:        
    a = x.split('/')

    tmp = re.findall(r'\d+', a[0]) # extracting digits
    list2.append(a[0])
    list2.append(tmp[0] + a[1])

print(list2)

Upvotes: 2

venkata krishnan
venkata krishnan

Reputation: 2046

This can be implemented with simple string splits.

Since you asked the output with regex, here is your answer.

list1 = ['2300LO/LCE','2302KO/KCE']

import re
r = re.compile("([0-9]{1,4})([a-zA-Z].*)/([a-zA-Z].*)")
out = []
for s in list1:
  items = r.findall(s)[0]
  out.append(items[0]+items[1])
  out.append(items[2])

print(out)

The explanation for the regex - (4 digit number), followed by (any characters), followed by a / and (rest of the characters).

they are grouped with () , so that when you use find all, it becomes individual elements.

Upvotes: 1

Mark
Mark

Reputation: 92460

You can make a simple generator that yields the pairs for each string. Then you can flatten them into a single list with itertools.chain()

from itertools import product, chain

def getCombos(s):
    nums, code = re.match(r'(\d+)(.*)', s).groups()
    for pair in product([nums], code.split("/")):
        yield ''.join(pair) 

a = ['2300LO/LCE','2302KO/KCE']

list(chain.from_iterable(map(getCombos, a)))
# ['2300LO', '2300LCE', '2302KO', '2302KCE']

This has the added side benefit or working with strings like '2300LO/LCE/XX/CC' which will give you ['2300LO', '2300LCE', '2300XX', '2300CC',...]

Upvotes: 7

Related Questions