如何在正则表达式语句中删除额外添加的行

时间:2015-07-31 15:44:25

标签: c# regex json parsing

我正在尝试使用一个正则表达式,它将带一个字符串并将其划分为不同的组,以便我可以根据我的代码需要调用这些组。下面是我试图制作模式的字符串。此字符串来自SQL中的数据库,但我不确定以下字符串是否为JSON:

{"data":"","displayException":"Unable to authenticate user.  Please make sure you have an 
existing account.  If you do not, select Register to create an account.  Please contact 
1-234-567-8910 M-F 9a-5pm CT for further assistance.","exception":"UNABLE TO LOGIN",
"success":false}

以下是我正在研究的模式:

@"("(.*)"\:\"\"\,)(\"(.*)\"\:)(\"(.*)\")"

我遇到的问题是,当它匹配时,我会得到多行相同的东西。下面是一个如何匹配的拆分列表:

0. {
1. "data":"",
2. data
3. "displayException":"Unable to authenticate user. Please make sure you have an existing account. If you do not, select Register to create an account. Please contact 1-234-567-8910 M-F 9a-5pm CT for further assistance.","exception":
4. displayException":"Unable to authenticate user. Please make sure you have an existing account. If you do not, select Register to create an account. Please contact 1-234-567-8910 M-F 9a-5pm CT for further assistance.","exception
5. "UNABLE TO LOGIN","success"
6.  UNABLE TO LOGIN","success
7. :false}

我想要的基本上是一行类似于4,但中间没有引号,一个像6,但前面的“异常”不在4的末尾,第7行与6结合(如果你想如果不是我可以添加它很好)

所以我的问题是,在正则表达式中他是一种我可以删除那些不必要的行的方法,或者我的正则表达式是假的而且他们是更好的,或者我只是必须强制调出那些偶数?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

好吧,你应该解析你的JSON !!

但是,正如关于正则表达式的问题,这是一次尝试:

(?:(?:"([^"}]*)")|(\w+))\s*:\s*(?:(?:"([^"}]*)")|(\w+))

Regex live here.