如何利用正則表達式匹配花括號內的內容


匹配花括號內的內容

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();
}

參考: http://stackoverflow.com/a/16538131/701457


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM