0

how would one find the closest match string to another string

so for example

string1:val1 ... string1:val2 .... string2:val3 ...

should capture the closest string1 to string2 which would be "string1:val2"

  • string1 can appear any number of times.

  • the in between characters can be anything

michael
  • 298
  • 2
  • 17

1 Answers1

2

Easiest way is something like (?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

Output

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