正则表达式:如何从 "bitwise" 值之后的字符串中获取 "width" 和 "height" 值?

Regex: how to get the "width" and "height" value from this string which is right after "bitwise" value?

假设我有这个字符串:

,"mimeType":"video/mp4;+codecs=\"avc1.42001E,+mp4a.40.2\"","bitrate":353051,"width":640,"height":320,"lastModified":"1543659519195688","contentLength":"24469560","quality":"medium","fps":24,"qualityLabel":"360p","projectionType":"RECTANGULAR","averageBitrate":35300;codecs=\"avc1.64001F,+mp4a.40.2\"","bitrate":987359,"width":1280,"height":640,"lastModified":"1543660211977003","quality":"hd720","fps":24,"qualityLabel":"720p","projectionType":"RECTANGULAR

我需要提取 "bitrate" 字符串之后的所有宽度和高度对值。

注意字符串有两次宽度和高度。我需要两双:

"bitrate":353051,"width":640,"height":320

"bitrate":987359,"width":1280,"height":640

请注意,我只需要获取紧跟 "bitrate" 值的值。如果之前没有 "bitrate" 值,那我就不要了。

所以答案应该是:

640,320
1280,640

我把字符串粘贴在这里:

https://regex101.com/r/VXAyvV/2

您可以使用此正则表达式,它只会匹配 widthheight 紧跟 bitrate:

的值
"bitrate":\d+,"width":(\d+),"height":(\d+)

宽度和高度将分别在第 1 组和第 2 组中捕获。

Demo on regex101

您可以使用以下正则表达式。

"bitrate":(?=.*?"width":(?<width>\d+))(?=.*?"height":(?<height>\d+))

Demo

我使用 PCRE (PHP) 正则表达式引擎对其进行了测试,但它应该适用于所有支持正面前瞻的引擎,即大多数正则表达式引擎。

我使用了命名的捕获组,但这不是必需的,因为宽度总是首先被捕获(在 "bitrate" 匹配之后),即使它在字符串中的高度之后。 "bitrate""width" 以及 "bitrate""height" 之间可以有中间字段。

考虑以下字符串。

"qual":"360p","bitrate":987359,"wt":90,"width":1280,"fps":24,"height":640,"last":"154"

当与正则表达式匹配时,名为 width 的捕获组将保留 "1280",捕获组 height 将保留 "640".

正则表达式引擎执行以下操作。

"bitrate":       match '"bitrate":'
(?=              begin a positive lookahead
  .*?            lazily match 0+ chars other than newlines
  "width":       match '"width":'
  (?<width>\d+)  match 1+ digits and save to capture group 'width'
)                end positive lookahead
(?=              begin a positive lookahead
  .*?            lazily match 0+ chars other than newlines
  "height":      match '"height":'
  (?<height>\d+) match 1+ digits and save to capture group 'height'
)                end positive lookahead