Reputation: 243
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
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
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
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