private void SetPitch(string name) { // [変化記号]?[音度] // 1,-2,+3,--4 // [+|-]*[1-9][0-9]? // 定数から正規表現パターン文字列を作成する(と思ったが、-だけ\を付与せねばならないため面倒。断念。) // string acc_chr = @""; // // foreach (char c in new char[]{Accidental.Flat.Unicode, Accidental.Flat.Ascii, Accidental.Flat.Operator, Accidental.Sharp.Unicode, Accidental.Sharp.Ascii, Accidental.Sharp.Operator}){acc_chr += c + '|';} //// foreach (char c in new char[]{Accidental.Flat.Unicode, Accidental.Flat.Ascii, Accidental.Flat.Operator, Accidental.Sharp.Unicode, Accidental.Sharp.Ascii, Accidental.Sharp.Operator}){acc_chr += c + "|";} // foreach (char c in new char[]{Accidental.Sharp.Operator, Accidental.Flat.Operator, Accidental.Sharp.Ascii, Accidental.Flat.Ascii, Accidental.Sharp.Unicode, Accidental.Flat.Unicode}){acc_chr += @"" + @c + @"|";} // acc_chr = @acc_chr.Substring (0, acc_chr.Length - 1); // System.Console.WriteLine (@acc_chr); // System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match (name, @"(?<accidential>["+@acc_chr+@"]*)(?<degree>[1-9][0-9]?)"); System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(name, @"(?<accidential>[+|\-|#|b|♯|♭]*)(?<degree>[1-9][0-9]?)"); int d = int.Parse(match.Groups ["degree"].Value); System.Console.WriteLine("deg:" + d); System.Console.WriteLine("a_s:" + match.Groups ["accidential"].Value); int a = Accidental.GetPitch(match.Groups ["accidential"].Value); System.Console.WriteLine("acc:" + a); this.Pitch = Degree.Pitchs [d] + a; System.Console.WriteLine("pit:" + this.Pitch); this.Name = name; }
private void SetPitch(string name) { // [変化記号]?[音度] // 1,-2,+3,--4 System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(name, @"(?<accidential>[+|\-|#|b|♯|♭]*)(?<degree>[1-9][0-9]?)"); int d = int.Parse(match.Groups ["degree"].Value); if (d < 1 || 14 < d) { throw new Exception("度数は1〜14の自然数にしてください。"); } int octavePitch = 0; if (7 < d) { octavePitch = 12; } int a = Accidental.GetPitch(match.Groups ["accidential"].Value); this.Pitch = Degree.Pitchs [(d < 8) ? d : d - 7] + a + octavePitch; this.Name = name; }