1
votes

I'm looking to match any text and non text values, located within " " , but only between the 1st and the last instance of [[ and ]]

window.google.ac.h(["text",[["text1",0],["text2",0,[3]],["text3",0],["text4",0,[3]],["text5",0],["text6",0],["text7",0 ]],{"q":"hygjgjhbjh","k":1}])

So far I've managed to get some results (far from ideal) by using: "(.*?)",0 The issue I have is the it either matches all the way until

"text4",0,[3]]

or starts matching at

["text",

I only need to match text1 text2 text3 .. text7

Notes: double square bracket position and nr of instances, between the 1st and the last is not consistent.

Thanks for your help guys!

Edit: I'm using http://regexr.com/ to test it

2
In which language/tool you're doing this?anubhava
Hi, I'm using regexr.com to test it. That's for Excel VBA, thanks.Sam
@Sam In that case my answer should work, because VBA uses the .NET regex engine.Adi Inbar
(?<=[[.*)"(.+?)"(?=.*]]) - VBA gives compile error'expected end of statement'and I'm unable to test using regexr.com The closest I've managed to get on regexr.com is:[.[a-z\\0-9 /.'_-]+.,(.|....)] , which seems to work in my VBA except in some cases (e.g. when the order of double brackets changes). I then use additional functions to clean it up, so, again, not ideal..Sam

2 Answers

2
votes

Well, you didn't say which language, but using .NET regex (which largely follows the Perl standard), the first match groups of all the matches of a global match using the following regex will contain the values you want:

(?<=\[\[.*)"(.+?)"(?=.*\]\])

A global match on "(.+?)" alone would return matches whose first match groups would contain the characters between the quotes. The lookbehind assertion (?<=\[\[.*) tells it to include only cases where there is a [[ anywhere behind, i.e. after the first instance of [[. Similarly, the lookahead assertion (?=.*\]\]) tells it to include only cases where there is a ]] anywhere ahead, i.e. before the last instance of ]].

I tested this using PowerShell. The exact syntax to do a global match and extract the first match groups of all the results will depend on the language.

0
votes

Depending on your RegEx Engine, you could use this pattern

(?:^.*?\[\[|\G[^"]*)("[^"]*")(?=.*\]\])

Demo