1

I need to retrieve all keys using pure regex. I need to start with word field and after that need to capture all multiple keys..

field[somevalue][anothervalue]

I'm using this regex: /^field(?=\[(\w+)](?=\[(\w+)])?)/

But I can retrieve only two levels (somevalue and anothervalue)

I need to go deep and retrieve another levels like:

field[somevalue][anothervalue][other][some]...

So I need to retrieve somevalue, anothervalue, other, some and so on, starting with variable name field

I don't want to use loops like this (Regex for bracket notation of multi-dimensional array)

Need to pass directly and use in https://regex101.com

4
  • With pcre like this with a capture group? (?:field|\G(?!\A))\[([^][]*)\] See regex101.com/r/DaDXh3/1 or (?:field|\G(?!\A))\[(\w+)\] Commented Oct 10, 2023 at 22:32
  • What exactly are you trying to achieve and what do you want to do with the results? With what programming language do you want to use the results? Regex101 is good but it is not a programming language. Commented Oct 10, 2023 at 22:33
  • This will work with five brackets but you can add more if you want them: ^field(?:\[(\w+)\])(?:\[(\w+)\])?(?:\[(\w+)\])?(?:\[(\w+)\])?(?:\[(\w+)\])? regex101.com/r/LGoFoQ/1 Commented Oct 10, 2023 at 22:39
  • I realize you are looking for a regex here, but I would like to point out that if you were to scan for matches of (?:\bfield|(?<=\[).*?(?=])) you could drop "field" and any matches that preceded it. Commented Oct 11, 2023 at 8:39

3 Answers 3

2

Using pcre, if "field" should be present, you can make use of a capture group and the \G anchor capturing in the word characters between the square brackets:

(?:\bfield|\G(?!\A))\[(\w+)\]

The pattern matches:

  • (?: Non capture group for the alternatives:
    • \bfield A word boundary, then match "field"
    • | Or
    • \G(?!\A) Assert the current postion at the end of the previous match, not at the start of the string
  • ) Close the non capture group
  • \[(\w+)\] Capture 1+ word chars between square brackets in group 1

See a regex demo.

Or a bit more broader match using a negated character class not matching [ and ] in between the square brackets:

(?:\bfield|\G(?!\A))\[([^][]*)\]

See another regex demo.

Sign up to request clarification or add additional context in comments.

Comments

1

Start the match with either a look behind for field or the end of the previous match (\G):

(?<=field|\G)\[(.*?)]

See live demo.

3 Comments

There's a problem if the string were, for example, "[cat]field[somevalue][anothervalue][other][some][aaa][bbb]", in which case "cat" would be matched (as well as "somevalue", "anothervalue", etc.) even though "[cat]" precedes "field". Demo 1. I believe you need the following or similar: (?:(?<=field)|(?!^)\G)\[(.*?)]. Demo 2
@CarySwoveland not according to these two sites: regexr.com/7lg2i and rubular.com/r/CkTIKDlTSDKOdj . I didn't try other sites. Maybe you can find one?
I don't follow. If I add "[cat]" to the beginning of the test string, before "field", "cat" is matched and captured at both those links in your comment. I understand only those strings enclosed in brackets that follow "field" are to be extracted (not "cat").
0

Here are two more approaches that could be taken if it were known that "field" were present in the string.

#1 Use a negative lookahead

(?<=\[)[^]]*(?=])(?!.*\bfield)

Demo

#2 Match unwanted strings, match and capture wanted strings

.*\bfield\[|\]\[|([^\]]+)

Demo

With this approach strings that are matched but not captured are discarded; the only matches of interest are those that are (matched and) captured (to capture group 1).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.