Hello everyone,
I am trying to find a certain substring in a given string using an regular expression. As an example, I've been using one of the JSON examples on this site, but already stripped from any insignificant whitespace (i.e. whitespace not inside quotes). My aim is to find the first word that is enclosed in quotes an followed by a colon. For that, I am using
Unfortunately, my code below doesn't work - it crashes, because apparently there's no match to be found:
I would have expected it to find
Does anybody have a clue what I am doing wrong here?
Thanks!
I am trying to find a certain substring in a given string using an regular expression. As an example, I've been using one of the JSON examples on this site, but already stripped from any insignificant whitespace (i.e. whitespace not inside quotes). My aim is to find the first word that is enclosed in quotes an followed by a colon. For that, I am using
as regex pattern."\w+":
Unfortunately, my code below doesn't work - it crashes, because apparently there's no match to be found:
find match using regex:
Dim source As String = $"{"menu":{"id":"file","value":"File","popup":{"menuitem":[{"value":"New","onclick":"CreateNewDoc()"},{"value":"Open","onclick":"OpenDoc()"},{"value":"Close","onclick":"CloseDoc()"}]}}}"$
Dim m As Matcher = Regex.Matcher2($""\w+":"$, source) ' Find occurances of a word that has quotes around it and is being followed by a colon
Dim match As String
match = m.Match
Log (match.SubString2(1, match.Length -2)) ' Return it without the quotes and the colon
I would have expected it to find
(and all the others) and thus log"menu":
as first match. Sadly, it doesn't And interestingly, the very same string gets parsed just as expected using the same regex pattern when doing so via Erel's interactive tool: https://b4x.com:51041/regex_ws/index.html:menu
Does anybody have a clue what I am doing wrong here?
Thanks!