Search code examples
regexregex-groupregex-greedy

Get all Content thats between Square Brackets: Regex


I have the follwing string:

"[parameters('ECC_Shipment_Trigger_properties_pl_ecc_shipment_adls_sql_parameters_File_system')]"

and I would like to write a regular expression that return only:

parameters('ECC_Shipment_Trigger_properties_pl_ecc_shipment_adls_sql_parameters_File_system')

Keeping in mind that I only need content that starts as:

parameters('some string')

So far I have come up with (?<=[parameters(').+?(?=')) which only returns the text between single quotes.

for testing, I am using https://regexr.com/

How can I modify my current regex to get this result?


Solution

  • The positive lookbehind is an assertion (non consuming), so this part (?<=\[parameters\(') means that from the current position it asserts what is on the left is [parameters(.

    Instead of a lookaround, you could use a capturing group and a negated character class matching any char except a ' instead.

    The value is in the first capturing group.

    \[(parameters\('[^']+'\))\]
    

    Regex demo

    If you want to use lookaround, this might be an option, asserting what is on the left is [ and what is on the right is ]

    (?<=\[)parameters\('[^']+'\)(?=\])
    

    Regex demo