Single Regex with multiple match patterns

Regex is hard 🙂

What I have right now is a string value that I am succesfuly matching values on specific keys. I need to expand my regex to match a using a value rather than a key.

The current Regex being applied is


What I want to add/extend is to match any values that contain an @ symbol. I wont know what the ‘key’ is in as its dynamic so I cant just add ‘badKey’ into the matched pattern. So an example input for this would be:

Basically I want to match all the existing parts and then also the ‘badKey’ one. I know I can run the string through a second Regex but for performance sake I would like this to be a single pattern.

Any help here would be appreciated.

>Solution :

You can modify your existing regular expression to include a second match for any unknown keys that contain values with an ‘@’ symbol by using a positive lookahead assertion with the ‘|’ operator. Here’s an example regular expression that should work for your use case:


Let’s break down the regex:

enter image description here

This regular expression should match all the existing parts as well as the ‘badKey’ parameter with its value containing an ‘@’ symbol.

Leave a Reply