匹配花括号内的内容
Input: {abc}, Output: abc
正则表达式: (?<=\{)[^}]*(?=\})
(?<=\{) 匹配以左花括号开头
[^}]* 取得内容
(?=\}) 匹配以右花括号结束
private List<String> GetTokens(String str)
{
Regex regex = new Regex(@"(?<=\{)[^}]*(?=\})", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(str);
// Results include braces (undesirable)
return matches.Cast<Match>().Select(m => m.Value).Distinct().ToList();
}
本文介绍了一个使用正则表达式从字符串中提取花括号内内容的具体案例。通过匹配特定模式,该示例展示了如何利用.NET框架下的Regex类实现这一功能。此方法对于处理配置文件或解析特定格式的数据特别有用。
1万+

被折叠的 条评论
为什么被折叠?



