2017-02-13 76 views
0

如何將一個找到最接近的匹配串到另一個串比賽最接近值對

因此,例如

字符串1:VAL1 ...字符串1:VAL2 ....字符串2:VAL3 ...

應該捕獲最接近STRING1至字符串2這將是「字符串1:VAL2」

  • 字符串1可以出現任意次數。

  • 字符之間的可以是任何東西

+0

參見['。* 「稱號」 :(。 *) 「值」:'](https://regex101.com/r/Ctu9h8/1)。 –

+0

@WiktorStribiżewthere coud be unlimited#前面的第一個字符串# –

+0

@WiktorStribiżew最後一個「title」:「」,它最接近「value」:「」 –

回答

2

最簡單的方法是一樣的東西(?s)"title":(?:(?!"title":).)*?"value":".*?"

(?s)     # Dot-all modifier 
"title":    # Literal 'title' 
(?:      # Group start 
     (?! "title":)   # Negative assertion, not 'title' ahead 
     .      # Ok, grab this chakracter 
)*?      # Group end, do 0 to many times 
"value":" .*? "   # Until 'value' is found 

輸出

** Grp 0 - (pos 73 , len 39) 
"title":"text2" asjdsjsd "value":"val1" 
+0

什麼是(?s)?你能解釋一下這個嗎?謝謝 –

+0

這是一個正則表達式結構calle d _Inline Modifier_。 's'表示Dot-all =(。)匹配包括換行符在內的任何字符。 – sln

+0

什麼是(?!)會有幫助,如果你可以解釋步驟,如果可能 –