代码之家  ›  专栏  ›  技术社区  ›  sudoExclamationExclamation

Regex:如何从这个字符串中获取“width”和“height”值,这个字符串正好在“bitwise”值之后?

  •  0
  • sudoExclamationExclamation  · 技术社区  · 6 年前

    假设我有这个字符串:

    ,"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
    

    “比特率” 价值。如果没有 “比特率”

    640,320
    1280,640
    

    我把绳子贴在这里了:

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Nick SamSmith1986    6 年前

    您可以使用这个regex,它将只匹配 width height bitrate :

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

    宽度和高度将分别记录在第1组和第2组中。

    Demo on regex101

        2
  •  0
  •   Cary Swoveland    6 年前

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

    Demo

    我用PCRE(PHP)正则表达式引擎测试了它,但是它应该可以与所有支持正lookaheads的引擎一起工作,正lookaheads是大多数正则表达式引擎。

    "bitrate" 是匹配的),即使它遵循字符串中的高度。中间可能有场 “比特率” "width" “比特率” "height" .

    考虑下面的字符串。

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

    当与regex匹配时,名为 width 将保持 "1280" height 将保持 "640"

    regex引擎执行以下操作。

    "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