1

I am using this regex .*\?(a\=([0-9]{3,4})) to match any URLs with the query string ?a={{a random number here}}. It should only match a query string with 3 or 4 digits

E.g.

http://www.test.com/test/test-test-test/?a=12
http://www.test.com/test-test/news/?a=734
http://www.test.com/test/?a=0987
http://www.test.com/test/test-test-test/?a=90235

My regex should match the second and third links as they contain a query string 3 or 4 digits.

I am using this tool http://www.regexpal.com/ and so far, it only highlights the second link.

2 Answers 2

2

Try this regex:

.+\/\?a=([0-9]{3,4})$
  • /.+\/\?a=([0-9]{3,4})$/gm
  • .+ matches any character (except newline)
  • Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
  • \/ matches the character / literally
  • \? matches the character ? literally
  • a= matches the characters a= literally (case sensitive)
  • 1st Capturing group ([0-9]{3,4})
  • [0-9]{3,4} match a single character present in the list below Quantifier: {3,4} Between 3 and 4 times, as many times as possible, giving back as needed [greedy]
  • 0-9 a single character in the range between 0 and 9
  • $ assert position at end of a line

See it working here: https://regex101.com/r/sP0lR3/1

And as a visual representation:

Regular expression visualization

Debuggex Demo

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

1 Comment

Thanks a lot! The visual representation was super helpful too. I will check the debuggex demo.
1
a=\d{3,4}$

Regex101 Demo and Explanation

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.