Home > Software design >  Regex for json path selector
Regex for json path selector

Time:01-15

I am trying to write json path select regex but not getting much idea.

Example:

Input text: "$path.child1.child2 $path.child1.child3" should give me 2 matches for "$path.child1.child2" and "$path.child1.child3". Please note these are not json value selector regex.

Json path level can be different from 1 to many.

CodePudding user response:

How about this: \$\w (?:\.\w )* https://regex101.com/r/NJHyCc/1

It will match $pn.c1 $pn.c1.c2 $pn.c1.c2.c3.c4 etc.. If you only want to specify a specific path name then replace \$\w with \$pathname.

CodePudding user response:

Assuming your paths are always separated by , the following regex will pull out the JSON paths in between:

https://regex101.com/r/Ru5Na6/1

(. ?)(?:(?:\s*\ \s*)|$)
  • Related