我試圖分裂歌詞與部分name = group(1)和lyrics = group(2)使用正則運算式:
#Chorus <-- section name [group(1)]
This is the chorus <-- section lyrics [group(2)]
Got no words for it <-- [group(2)]
#Verse <-- next section name
This is the verse <-- next section lyrics
我設法將第一次出現的group(1)與其余的分開,但它將所有其他出現的group(2).
List<SongSection> _sections = [];
RegExp regExp = RegExp(r'\n#([a-zA-Z0-9] )\n((.|\n)*)', multiLine: true);
List<RegExpMatch> matches = regExp.allMatches('\n' lyrics).toList();
for (RegExpMatch match in matches) {
_sections.add(
SongSection(
name: match.group(1)!,
lyrics: match.group(2)!.trim(),
),
);
}
print(_sections.toString())
OUTPUT:
[SongSection(name: 'Chorus', lyrics: 'This is the chorus\nGot no words for it\n\n#Verse\nThis is the verse')]
我怎樣才能始終將所有內容與下一次匹配group(1)?
uj5u.com熱心網友回復:
您可以匹配名稱的 # 和允許的字符,并為歌詞捕獲所有不以名稱模式開頭的行。
由于前導換行符是第 2 組的一部分,您可以隨后將其從第 2 組值中洗掉。
#([a-zA-Z0-9] )((?:\n(?!#[a-zA-Z0-9] $).*)*)
#([a-zA-Z0-9] )匹配#并捕獲第 1組中列出的 1 個或多個字符(捕獲組 2(?:非捕獲組\n匹配換行符(?!#[a-zA-Z0-9] $)Assert not#and 1 or more list characters to right-.*` 匹配整行
)*關閉非捕獲組并可選擇重復它
)關閉第 2 組
正則運算式演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/387088.html
