简体   繁体   English

排除单词但在正则表达式中包含字符

[英]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.我想提取和弦名称, <Chord:> ,还包括逗号。 So, for that string, I want to match Aaug, C#aug/A, Faug/A .所以,对于那个字符串,我想匹配Aaug, C#aug/A, Faug/A

I have tried this regex: \\b(?:(?!Chord:)\\w)+\\b .我试过这个正则表达式: \\b(?:(?!Chord:)\\w)+\\b This excludes the word Chord , but does not match # and / .这排除了单词Chord ,但不匹配#/

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.说明:<Chord:直到第一次出现>以及>匹配它,如果你想匹配python中的所有出现,你可以在python3中使用re库的findall

With python demo:使用 python 演示:

To catch values between each <Chord: to > try:要捕获每个<Chord: to >之间的值,请尝试:

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 > :或将值与<Chord:>匹配:

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>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM