我正在尝试在格式化为吉他/歌词的标准文本文档中为和弦添加方括号,以使它们与OnSong应用程序更兼容。我有规则,但不明白如何匹配所有可能的组合。规则是:
一些注意事项:这是一个帮助脚本......不需要完美。我现在手工做,所以偶尔的错过也没关系。我不是要解析和弦的细节,只是将它们包裹在[]中。虽然标准布局是1行和弦,1行歌词,但这不能指望,所以我知道有些场景会偶尔失败。
测试源(为了测试目的,和弦是随机的,以防任何音乐家打算在可怕的音乐上插话):
Db Dsus4/F# A Cbmin/C
A man can't be asked for that much to do
D/F# G A D#/E
And I can't sweep you off of your feet
应该变成:
[Db] [Dsus4/F#] [A] [Cbmin/C]
A man can't be asked for that much to do
[D/F#] [G] [A] [D#/E]
And I can't sweep you off of your feet
我的第一次尝试让我接近:
([A-G]((?!\s).)*)
但是那些从那些字母开始的单词也是如此。我现在已经四处走动,只能到达:
\b([CDEFGAB](#|##|b|bb|sus|maj|min|aug)?\b)
当我尝试使用[^\s+]
时,我会得到混合的结果,这些结果可以提供更多我想要的东西,但也会放弃我需要的东西。我想我只是在脑子里。任何帮助都会非常感激,任何有关它如何工作的解释都会更好。虽然我想要一个解决方案,但我也非常想解释它为何有效......
答案 0 :(得分:6)
这会使用您的样本输入,并达到您所有的“超级奖励积分”要求:
String output = input.replaceAll("(?m)(^| )([A-G](##?|bb?)?((sus|maj|min|aug|dim)\\d?)?(/[A-G](##?|bb?)?)?)( (?!\\w)|$)", "[$2]");
此代码将此变为此(作为包含嵌入行费用的单个字符串):
Db Dsus4/F# A Cbmin/C
A man can't be asked for that much to do
D/F# G A D#/E
And I can't sweep you off of your feet
进入这个:
[Db] [Dsus4/F#] [A] [Cbmin/C]
A man can't be asked for that much to do
[D/F#] [G] [A] [D#/E]
And I can't sweep you off of your feet
答案 1 :(得分:3)
我为你提供的案例提供了一些工作正则表达式,但不确定它对其他人有用。问题是一行可以以A
开头,也可以在歌曲行中。我尝试使用负前瞻检查它是否后跟一个空格和一个字母数字。如果有空格和字母数字,我们与此和弦不匹配。由于和弦可以在/
之后重复,我的模式加倍。
\b([CDEFGAB](?:b|bb)*(?:#|##|sus|maj|min|aug)*[\d\/]*(?:[CDEFGAB](?:b|bb)*(?:#|##|sus|maj|min|aug)*[\d\/]*)*)(?=\s|$)(?! \w)
查看the demo。
答案 2 :(得分:2)
我已经从前一个答案中得到了一些改进,以帮助解决我的问题。现在它忽略了一些"和弦可能"当它在诗节的开头(如A,E)。 这就是我出来的:
if(vc === viewController) )
{
return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
}
答案 3 :(得分:2)
检查一下:
/([A-G](#|b)?)(\(?(M|maj|major|m|min|minor|dim|sus|dom|aug)?(\+|-|add)?\d*\)?)(\/([A-G](#|b)?))?/g
我从chord-transposer那里拿走了:
var XRegExp = require("xregexp");
// Chromatic scale starting from C using flats only.
var FLAT_SCALE = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "Cb"];
// Chromatic scale starting from C using sharps only.
var SHARP_SCALE = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
// Regex for recognizing chords
var ROOT_PATTERN = '(?<root>[A-G](#|b)?)';
var SUFFIX_PATTERN = '(?<suffix>\\(?(M|maj|major|m|min|minor|dim|sus|dom|aug)?(\\+|-|add)?\\d*\\)?)';
var BASS_PATTERN = '(\\/(?<bass>[A-G](#|b)?))?';
var MINOR_PATTERN = '(m|min|minor)+';
var CHORD_REGEX = XRegExp("^" + ROOT_PATTERN + SUFFIX_PATTERN + BASS_PATTERN + "$");
然后
console.log(CHORD_REGEX); // will output regexp mentioned at the beginning of the answer
对我来说很好。
答案 4 :(得分:0)
代码执行协议很好地在我检查的样本和协议简单第7条协议与尖锐或扁平像C#7
string strRegex = @"^[A-G]([5679bm#]([57])?|1[13]|6\/9|7[-#+b][59]|7?sus[24]|add[249]|aug|dim7?|m\/maj7|m1[13]|m[679]|m7?b5|maj1[13]|maj[79])?([\/][A-G]([5679bm#])?([57])?)?";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"A";
string strReplace = @"[$0]";
return myRegex.Replace(strTargetString, strReplace);