首页 > 解决方案 > 如何在字符串中搜索文本并抓取搜索文本之后的所有内容,直到它到达一个字符?

问题描述

我正在网络上搜索一个巨大的 html 文档,其中包含多个名称实例。整个页面源代码的每个部分都将包含类似这样的内容

{"keyword_text":"kathy smith","item_logging_id":"2021-05-16:yakMrD","item_logging_info":"{"source":"entity_bootstrap_connected_user_suggestion",{"keyword_text":"courtney lee","item_logging_id ":"2021-05-16:lX1LC2","item_logging_info":"{"source":"entity_bootstrap_connected_user_suggestion",

我想获取源中的所有名称并将它们放入文本框中。

在字符串中搜索 "keyword_text":" 然后抓取所有文本,直到它到达 " 不包括 "

我希望最终结果是

kathy smith

courtney lee

标签: c#regexstringsplit

解决方案


Considering the "until it reaches the character" you can use the regex ([^\"]*)

^ means NOT and * means multiple times. So it reads everything until the first appearance of ". The \ is to escape the quotes.

So in your case this is the regex: \"keyword_text\":\"([^\"]*) to get the name-part without quotes.

And in c# context:

var matches= new Regex("\"keyword_text\":\"([^\"]*)").Matches(yourInputText);

foreach (Match match in matches)
{
    Console.WriteLine(match.Groups[1].Value);
}

推荐阅读