如何获取一下字符串内的值
--Property Property = { background = {flag=2,color={0,0,0},url="background.jpg"}, height = 92, keep = {x = 10, y = 0}, recommend = {x = 480, y = 0}, search = {x = 570, y = 80}, searchText = { x = 0, y = 92, height=73, text = { "1","2","3"}, size = 38, color = {54, 106, 162}, offsetX = 10, spacing = 20, } }
要得到的结果是外层大括号内所有信息,注意总大括号中有包含其他大括号,该如何实现?
把=替换成:
不就是json数据了吗?
用System.Runtime.Serialization.Json.DataContractJsonSerializer
或System.Web.Script.Serialization.JavaScriptSerializer
解析json数据就可以了
string str = File.ReadAllText("D:\\1.txt", Encoding.Default);
var ary = Regex.Matches(Regex.Replace(str, @"^[^{]+{|}[^}]+$", ""), @"(?is)([a-z]+)\s*=\s*{(((?<g>{)|(?<-g>})|[^{}])*(?(g)(?!)))}")
.OfType<Match>().Select(t => new { name = t.Groups[1].Value, txt = t.Groups[2].Value }).ToArray();