试试这个正则表达式:
([a-z_]\w*)_([a-z\d]+)
火柴:
Some_Button1_Click
Button1_Click
Some_strange_Button1_Click
示例代码:
const string strRegex = @"([a-z_]\w*)_([a-z\d]+)";
const RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string text = @"Some_Button1_Click";
var matches = myRegex.Matches(text)
.Cast<Match>()
.Select(x => new {
AllText = x.Groups[0], // all text ==> "Some_Button1_Click"
Object = x.Groups[1], // ([a-z_]\w*) ==> "Some_Button1"
Event = x.Groups[2] // ([a-z\d]+) ==> "Click"
})
.ToList();