簡體   English   中英

正則表達式:匹配兩個最接近的字符串,它們之間僅出現另一個字符串

[英]Regex : Match the two closest strings with only one occurence of another string between them

我想做兩行的非貪婪匹配,但只包括它們之間另一行的出現。

讓我用 ffprobe 的日志來說明這一點:

 [FRAME] media_type=video stream_index=1 pict_type=P coded_picture_number=1 display_picture_number=0 [/FRAME] [FRAME] media_type=video stream_index=1 pict_type=B coded_picture_number=1 display_picture_number=0 [/FRAME] [FRAME] media_type=video stream_index=1 pict_type=P coded_picture_number=1 display_picture_number=0 [/FRAME] [FRAME] media_type=video stream_index=1 pict_type=I coded_picture_number=1 display_picture_number=0 [/FRAME] [FRAME] media_type=video stream_index=1 pict_type=P coded_picture_number=1 display_picture_number=0 [/FRAME] [FRAME] media_type=video stream_index=1 pict_type=I coded_picture_number=1 display_picture_number=0 [/FRAME]

該日志由視頻幀組成。 幀以 [FRAME] 開始,以 [/FRAME] 結束。

我想在下一幀中匹配 pict_type=B 緊跟 pict_type=I 。

顯然pict_type=B.*?pict_type=I不在這里工作,它會匹配 BPI

我試圖將 [/FRAME] 的出現限制為僅 1

pict_type=B(.*?[^\[\/FRAME\]]{1})pict_type=I

但它匹配兩個 [/FRAME] 只是為了達到 pict_type=I

此外,每個視頻都有不同數量的行,所以用 \r\n 重復做一個正則表達式行是沒有用的

我做錯了什么,我怎么能告訴它在我的兩個 pict_type 之間只允許一個 [/FRAME]?

您可以使用與開始[FRAME]和結束[/FRAME]匹配的模式,並且在匹配第一個pict_type=B然后在下一幀中匹配第二個pict_type=I時,使用負前瞻(?!

\[FRAME](?:\R(?!\[/?FRAME]|pict_type).*)*+\Rpict_type=B(?:\R(?!\[/?FRAME]|pict_type).*)*+\R\[/FRAME]\R\[FRAME](?:\R(?!\[/?FRAME]|pict_type).*)*+\Rpict_type=I(?:\R(?!\[/?FRAME]|pict_type).*)*+\R\[/FRAME]

模式匹配:

  • \[FRAME]匹配[FRAME]
  • (?:\R(??\[/.FRAME]|pict_type).*)*+使用所有格量詞匹配所有不以[FRAME][/FRAME]pict_type的行*+
  • \Rpict_type=B匹配換行符和pict_type=B
  • (?:\R(??\[/.FRAME]|pict_type).*)*+匹配所有不以[FRAME][/FRAME]pict_type
  • \R\[/FRAME]匹配換行符並匹配結束符[/FRAME]
  • \R\[FRAME]匹配換行符並匹配第二個[FRAME]
  • (?:\R(??\[/.FRAME]|pict_type).*)*+匹配所有不以[FRAME][/FRAME]pict_type
  • \Rpict_type=I匹配換行符並匹配pict_type=I
  • (?:\R(??\[/.FRAME]|pict_type).*)*+匹配所有不以[FRAME][/FRAME]pict_type
  • \R\[/FRAME]匹配換行符並匹配[/FRAME]

正則表達式演示

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM