Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

https://www.me.com/?name=bob&identify=bob1&test=email@me.com&validKey=validValue

The current Regex being applied is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

((name|identify|test)(=|%3D)[^&]*)

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:

https://www.me.com/?name=bob&identify=bob1&test=email@me.com&validKey=validValue&badKey=test2@test.com

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:

(((name|identify|test)(=|%3D)[^&]*))|(((.)(?=[^&@]*@)[^&]*))

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading