在C#中使用正则表达式匹配特定字符

时间:2013-08-01 01:54:05

标签: c# regex

我有一个字符串:

"{
\"__type\":\"DailyRequestItem:#CapitalIQ.DataAPI.Download\",
\"Identifier\":{\"__type\":\"CompanyIdentifier:#CapitalIQ.DataAPI\",\"SearchString\":\"goog\"},
\"FormulaMetric\":{\"MetricName\":\"IQ_LASTSALEPRICE\",\"MetricId\":0},
\"ReturnType\":0,
\"CurrencyConversionInfo\":{\"CurrencyId\":160,\"CurrencyConversionMode\":0},\"Response\":{
\"Id\":0,\"CompanyId\":29096,\"TradingItemId\":11311662,\"RequestedCurrencyId\":0,\"CurrencyConversionModeId\":0,\"Error\":0,\"Values\":[{\"CurrencyConversionDate\":\"\\/Date(-62135578800000-0500)\\/\",\"DataTypeId\":2,\"ReportedCurrencyId\":0,\"Id\":0,\"CurrencyId\":160,\"ScaleId\":0,\"UnitsId\":0,\"ValueId\":0,\"ValueAsString\":\"896.598000\",\"Delimiter\":\",\",\"SubValueList\":null,\"Error\":0,\"ClickThroughTypeId\":0,\"InstanceId\":0,\"PricingDate\":\"\\/Date(1374724800000-0400)\\/\",\"ConversionMode\":0,\"AuditabilityTypeId\":0,\"AsOfDate\":\"\\/Date(1374724800000-0400)\\/\",\"CompanyId\":0,\"DataItemId\":0,\"TradingItemId\":0}]
},
\"RangeInfo\":{\"AsOfDate\":\"\\/Date(1374724800000-0400)\\/\",\"StartDate\":\"\\/Date(-62135596800000)\\/\",\"EndDate\":\"\\/Date(-62135596800000)\\/\",\"Frequency\":1,\"ReturnType\":0,\"TradingDayOffset\":null,\"IsIntraday\":false},\"Period\":null}"

我需要删除此部分:

\"Response\":{
\"Id\":0,\"CompanyId\":29096,\"TradingItemId\":11311662,\"RequestedCurrencyId\":0,\"CurrencyConversionModeId\":0,\"Error\":0,\"Values\":[{\"CurrencyConversionDate\":\"\\/Date(-62135578800000-0500)\\/\",\"DataTypeId\":2,\"ReportedCurrencyId\":0,\"Id\":0,\"CurrencyId\":160,\"ScaleId\":0,\"UnitsId\":0,\"ValueId\":0,\"ValueAsString\":\"896.598000\",\"Delimiter\":\",\",\"SubValueList\":null,\"Error\":0,\"ClickThroughTypeId\":0,\"InstanceId\":0,\"PricingDate\":\"\\/Date(1374724800000-0400)\\/\",\"ConversionMode\":0,\"AuditabilityTypeId\":0,\"AsOfDate\":\"\\/Date(1374724800000-0400)\\/\",\"CompanyId\":0,\"DataItemId\":0,\"TradingItemId\":0}]
},    

并序列化它。我正在使用这个正则表达式,但无济于事:

string pattern = @"\\Response\.\]}";

有人可以帮助我形成正则表达式吗?请帮忙

1 个答案:

答案 0 :(得分:1)

由于人们建议解析JSON响应正则表达式不是一个合适的解决方案,但如果您没有找到任何其他解决方案,您仍可以使用此模式来解决您的问题:

(\"Response\")[\S\s]*?(?=\"RangeInfo\")

看看这个rubular demo

注意:

我希望JSON响应始终采用特定格式,RangeInfo元素将成为JSON响应中的下一个节点。