当前位置: 代码迷 >> C# >> ,正则表达式的使用
  详细解决方案

,正则表达式的使用

热度:15   发布时间:2016-05-05 03:30:09.0
求助,正则表达式的使用?

        /// <summary>
        /// 获取两个字符串之间的字符
        /// </summary>
        /// <returns></returns>
        public static string[] GetValueAnds(string text)
        {
            try
            {
                string[] str;
                if (string.IsNullOrEmpty(text)) return null;
                MatchCollection mc;
                string regex = "^title.*\"$";
                Regex rgClass = new Regex(regex, RegexOptions.Singleline);
                mc = rgClass.Matches(text);
                str = new string[mc.Count];
                for (int i = 0; i < mc.Count; i++)
                {
                    str[i] = mc[i].Value;
                }
                return str;
            }
            catch { return null; }
        }


内容:
loadMenu([[],[],[{"valid":"Y","categoryid":"HG0001","title":"ELSWORD","contentid":"J_ES","status":"U","ment":"N","contenturl":"http://elsword.hangame.co.jp/"},{"valid":"Y","categoryid":"HG0002","title":"チョコットランド","contentid":"CHOCOTTO","status":"E","ment":"N","contenturl":"http://casual.hangame.co.jp/chocotto/top.nhn"},{"valid":"Y","categoryid":"HG0014","title":"オセロ","contentid":"OTHELLO","status":"","ment":"N","contenturl":"http://casual.hangame.co.jp/othello/"},{"valid":"Y","categoryid":"HG0009","title":"ハッピーベジフル","contentid":"O_PWZ","status":"E","ment":"N","contenturl":"http://casual.hangame.co.jp/o_pwz/index.nhn"},{"valid":"Y","categoryid":"HG0001","title":"ドラゴンネスト","contentid":"J_DNEST","status":"E","ment":"N","contenturl":"http://static.hangame.co.jp/r02/game/dragonnest/lp/2015/t01/index.html"}]]);

我想把title里面的内容提取出现,但怎么弄不行,求助各位大神!!
------解决思路----------------------
格式比较简单,这样就行
public static string[] GetValueAnds(string text)
{
    try
    {
        string[] str;
        if (string.IsNullOrEmpty(text)) return null;
        MatchCollection mc;
        string regex = "\"title\":\"([^\"]*)\"";
        Regex rgClass = new Regex(regex, RegexOptions.Singleline);
        mc = rgClass.Matches(text);
        str = new string[mc.Count];
        for (int i = 0; i < mc.Count; i++)
        {
            str[i] = mc[i].Groups[1].Value;
        }
        return str;
    }
    catch { return null; }
}