shjnlee
shjnlee

Reputation: 243

Using regex, get a string after a character, special case

I'm using python with regex to read a file and get a list of string after a character (/).

For example:

text = "Youngblood /Pop Midnight/R&B Thunder/Rock"
re.findall(r'/([^\s]+)', text)

would return

['Pop', 'R&B', 'Rock']

Now, let's say if there is a special case where there is two characters '/' in a substring,

abc\/def/this

or

abc\/def\/ghi/this

the regex will not work correctly (as I wanted) and return 'def/this' or 'def/ghi/this'.

The correct output is "this". How do I fix the regex to work with that special case?

Upvotes: 0

Views: 70

Answers (3)

Jongware
Jongware

Reputation: 22457

re.findall(r'/([^\s/]+(?!\S))', text)

returns

['Pop', 'R&B', 'Rock', 'ghj']

You need a lookahead because you want to inspect the character after your match. You cannot use (?=\s) (which will match the space) because then the last item will be skipped. The construction (?!\S) means not not a space, which matches a space but also End Of String.

Upvotes: 3

Rocky Li
Rocky Li

Reputation: 5958

If you're not hell bent on using all regex to complete the exact purpose, you can combine what you already have with this list comprehension:

targets = [e.split('/')[-1] for e in re.findall('/([^\s]+)', text)]

Upvotes: 0

Raoslaw Szamszur
Raoslaw Szamszur

Reputation: 1740

If there is always space between strings you can do it even without regexp.

text = "Youngblood/Pop Midnight/R&B Thunder/Rock abc/def/ghj"
output = []

for item in text.split(" "):
    output.append(item.split("/")[-1])

Output:

['Pop', 'R&B', 'Rock', 'ghj']

Upvotes: 1

Related Questions