0

I have this string:

"var block = new MatchesBlock('page_competition_1_block_competition_matches_summary_5', 'block_competition_matches_summary', {"page":0,"bookmaker_urls":{"13":[{"link":"http:\/\/www.bet365.com\/home\/?affiliate=365_178981","name":"Bet 365"}]},"block_service_id":"competition_summary_block_competitionmatchessummary","round_id":42011,"outgroup":false,"view":2,"competition_id":13});"

I want get:

{"page":0,"bookmaker_urls":{"13":[{"link":"http:\/\/www.bet365.com\/home\/?affiliate=365_178981","name":"Bet 365"}]},"block_service_id":"competition_summary_block_competitionmatchessummary","round_id":42011,"outgroup":false,"view":2,"competition_id":13}

so I write:

MatchesBlock\(([^)]+)\)

but this will take all the string inside MatchesBlock

fantaghiro
  • 93
  • 1
  • 8
  • Check out the answer to this question: https://stackoverflow.com/questions/21994677/find-json-strings-in-a-string I hope it helps. – ggluta Apr 21 '18 at 17:36
  • @GeorgeGabriel I use `\{(?:[^{}]|(?R))*\}` won't work – fantaghiro Apr 21 '18 at 17:39
  • 3
    Please tag your question with a lang or a tool. – revo Apr 21 '18 at 17:40
  • In the general case, you cannot do this; JSON is not a regular language and cannot be parsed with regular expressions. If the JSON happens to be the only place that curly braces show up and it will always be a JSON object at the top level, then it's possible, but we need more details to know that. – Daniel H Apr 21 '18 at 17:51

3 Answers3

0

You can use the fact that regex is greedy by default and use

MatchesBlock[^\{]+\K(.*})

Here I use \K to reset the match, i.e. anything before \K will not be captured.

builder-7000
  • 7,131
  • 3
  • 19
  • 43
  • This doesn't do what he asked. He does **NOT** want `MatchesBlock` or the other parameters to be captured. He only wants the JSON. – Jake Miller Apr 21 '18 at 17:44
  • How did you know that OP's end regex flavor supports `\K`? – revo Apr 21 '18 at 18:10
  • As you correctly mentioned in other comment, I'd suggest the OP to post his/her prog. language. OP can determine if `\K` is supported from this post: https://stackoverflow.com/questions/13542950/support-of-k-in-regex – builder-7000 Apr 21 '18 at 18:22
  • OP said in his / her comment he / she can't have that regex to work. If that regex does not work for him / her then `\K` wouldn't work either. – revo Apr 21 '18 at 18:22
0

If you only wish to extract the JSON from your MatchesBlock constructor parameters, simply use the following regex:

\{(?:[^{}]|(?R))*\}

Input:

var block = new MatchesBlock('page_competition_1_block_competition_matches_summary_5', 'block_competition_matches_summary', {"page":0,"bookmaker_urls":{"13":[{"link":"http:\/\/www.bet365.com\/home\/?affiliate=365_178981","name":"Bet 365"}]},"block_service_id":"competition_summary_block_competitionmatchessummary","round_id":42011,"outgroup":false,"view":2,"competition_id":13});

Output:

{"page":0,"bookmaker_urls":{"13":[{"link":"http:\/\/www.bet365.com\/home\/?affiliate=365_178981","name":"Bet 365"}]},"block_service_id":"competition_summary_block_competitionmatchessummary","round_id":42011,"outgroup":false,"view":2,"competition_id":13}
Jake Miller
  • 2,432
  • 2
  • 24
  • 39
0

For the example provided, you can use:

{[^)]+

enter image description here

Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268