C# 正则表达式解析字符串

方法

public static void ReadBasicInfo(string Infostr, Dictionary<string, int> InfostrDictionary, System.Windows.Forms.GroupBox box)
{
    try
    {
        Infostr = "{'sfzh': '230407199509070010','tjbh': '1591250069115','name': '6Z+p5bCP56eD','age': '24','sex': '1','xh': '1'}";
        if (!string.IsNullOrEmpty(Infostr) && Infostr.Contains("{") && Infostr.Contains("}"))
        {
            System.Text.RegularExpressions.MatchCollection mtsss = System.Text.RegularExpressions.Regex.Matches(Infostr, @"'sfzh': '?(?<sfzh>.+?)','tjbh': '?(?<tjbh>.+?)','name': '?(?<name>.+?)','age': '?(?<age>.+?)','sex': '?(?<sex>.+?)','xh': '");
            if (mtsss.Count != 0)
            {
                foreach (KeyValuePair<string, int> kv in InfostrDictionary)
                {
                    if (box.Controls.Count > 0)
                    {
                        for (int i = 0; i < box.Controls.Count; i++)
                        {
                            if (i == kv.Value && box.Controls[i].GetType().ToString().Equals("System.Windows.Forms.TextBox"))
                            {
                                ((System.Windows.Forms.TextBox)box.Controls[i]).Text = kv.Key.Equals("name") ? Encoding.UTF8.GetString(Convert.FromBase64String(mtsss[0].Groups[kv.Key].Value)) : mtsss[0].Groups[kv.Key].Value; break;
                            }
                        }
                    }
                }
            }
        }
    }
    catch (Exception)
    {
        return;
    }
} 

//网络素材仅限收藏 方便学习

posted @ 2021-06-08 21:09  New_HackerHK  阅读(157)  评论(0编辑  收藏  举报