Sederfo
Sederfo

Reputation: 199

Exclude words but include characters in regex

I have the following string:

<Chord: Aaug>, <Chord: C#aug/A>, <Chord: Faug/A>

I want to extract the chord names, what is between <Chord: and > and also include the commas. So, for that string, I want to match Aaug, C#aug/A, Faug/A.

I have tried this regex: \b(?:(?!Chord:)\w)+\b. This excludes the word Chord, but does not match # and /.

Upvotes: 1

Views: 463

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133750

Could you please try following, written and test in link demo of regex

<Chord:([^>]*)>

Explanation: From <Chord: till 1st occurrence of > along with > matching it, if you want to match all occurrences in python, you could use findall of re library in python3.

With python demo:

To catch values between each <Chord: to > try:

import re
a='<Chord: Aaug>, <Chord: C#aug/A>, <Chord: Faug/A>'
re.findall(r'<Chord: ([^>]*)', a)
['Aaug', 'C#aug/A', 'Faug/A']

OR to match values with <Chord: and >:

import re
a='<Chord: Aaug>, <Chord: C#aug/A>, <Chord: Faug/A>'
re.findall(r'<Chord:([^>]*)>', a)
[' Aaug', ' C#aug/A', ' Faug/A']
re.findall(r'<Chord:[^>]*>', a)
['<Chord: Aaug>', '<Chord: C#aug/A>', '<Chord: Faug/A>']

To access individual items from list:

re.findall(r'<Chord:[^>]*>', a)[0]
'<Chord: Aaug>'
re.findall(r'<Chord:[^>]*>', a)[1]
'<Chord: C#aug/A>'

Upvotes: 3

Related Questions