【问题标题】:How to parse a quoted string? [duplicate]如何解析带引号的字符串? [复制]
【发布时间】:2019-06-04 10:56:25
【问题描述】:

我有一个类似这样的字符串,这个字符串来自一个很长的 HTML 页面源:

"entity_id":"1234567890"

我正在尝试像这样解析数字1234567890,但无法解析id:

var re = new Regex("\"entity_id\":\"([0 - 9] +)\"");
var match = re.Match(task.Result);

var id = match.Success ? match.Value : string.Empty;
Console.WriteLine(id);

为什么它没有被解析以及如何修复它?

【问题讨论】:

  • 这看起来很像来自较大 JSON 内容的片段。这是 JSON 吗?
  • 不,它包含在页面 html 源代码中
  • 带正则表达式:var id = Regex.Match(s, "\"entity_id\":\"([0-9]+)\"")?.Groups[1].Value.
  • 干得好。工作

标签: c# regex


【解决方案1】:

去掉空格:

//var re = new Regex("\"entity_id\":\"([0 - 9] +)\"");
  var re = new Regex("\"entity_id\":\"([0-9]+)\"");

然后使用

 var id = match.Success ? match.Groups[1].Value : string.Empty;

【讨论】:

  • 干得好。作品
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多