欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

正则表达式匹配两个“标识点”之间的内容

程序员文章站 2024-03-24 18:24:40
...

示例说明

如何用正则取出所有的id值

{
	"state": 0,
	"data": [{
				"{
	"state": 0,
	"data": [{
				"id": "228937",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976802,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976882"
			}, {
				"id": "228920",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976423,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976521"
			}, {
				"id": "228919",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976393,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976521"
			}, {
				"id": "228915",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976066,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976101"
			}, {
				"id": "228914",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976066,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976101"
			}, {
				"id": "228593",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552890552,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552890661"
			}, {
				"id": "228592",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552890444,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552890481"
			}, {
				"id": "228591",
				"msg": "{\"fromAccountNick\228937",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976802,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976882"
			}, {
				"id": "228920",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976423,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976521"
			}, {
				"id": "228919",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976393,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976521"
			}, {
				"id": "228915",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976066,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976101"
			}, {
				"id": "228914",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552976066,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552976101"
			}, {
				"id": "228593",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552890552,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552890661"
			}, {
				"id": "228592",
				"msg": "{\"fromAccountNick\":\"游客CBD57****D8E75\",\"elems\":[{\"type\":\"TIMTextElem\",\"content\":{\"text\":\"love_msg\"}}],\"time\":1552890444,\"fromaccount\":\"游客CBD57****D8E75\"}",
				"fromaccount": "游客CBD57****D8E75",
				"dottime": "0:00",
				"parent_id": "",
				"time": "1552890481"
			}, {
				"id": "228591",
				"msg": "{\"fromAccountNick\

取出所有id值

其实就是取出 “id”:"","msg之间的内容
正则:(?<=("id":")).*?(?=","msg)
正则表达式匹配两个“标识点”之间的内容

取出所有id值包括前面的标识

正则:(?=("id":")).*?(?=","msg)
正则表达式匹配两个“标识点”之间的内容

取出所有id值包括前面的标识和后面的标识

正则:(?=("id":")).*?(?<=","msg)
正则表达式匹配两个“标识点”之间的内容

知识点

  1. (?<=标识A) 匹配一个位置,而不是字符.这个位置在 标识A 后面

  2. (?=标识B) 匹配一个位置,而不是字符.这个位置在 标识B 前面

  3. .* 这两个位置之间的所有字符.贪婪匹配模式

  4. .*? 这两个位置之间的所有字符.懒惰匹配模式,也叫非贪婪模式