{ "userData": { "user": { "id": "226549684", "shop_name": "密儿拆卡社", "user_name": "", "mobile": "", "status": 0, "create_time": 1748269764, "update_time": "2025-05-26 23:14:12", "agent": "", "user_type": 5, "address": "", "toutiao_id": "722805543169036", "collect_tel": "",
在上述获取到的HTML源码中,有一段JSON,如果直接用复制过来进行正则匹配,发现匹配不到,之间看似有空格间隔,但却仍然无法成功匹配。
string loginUserName = GetRegexValue(html, "\"shop_name\": \"(?.+?)\"");
string loginUserID = GetRegexValue(html, "\"toutiao_id\": "(?[0-9]+)\"");
那就有可能不是空格,换个思路用\s来表示非字符,这样就能成功匹配到了。
string loginUserName = GetRegexValue(html, "\"shop_name\"\\s*:\\s*\"(?.+?)\"");
string loginUserID = GetRegexValue(html, "\"toutiao_id\"\\s*:\\s*\"(?[0-9]+)\"");
正则JSON中的空格处理 踩坑备忘,搞了好长时间,只发现这个问题。