Windbrand123
Windbrand123

Reputation: 113

URL matching regex

I need some help with URL matching regex. I read the regex syntax documentation but it's so complex.

I'm trying to create a URL list for a checkout funnel, how would I set up regex for the following?

https://shop.mysite.ca/[unique ID]/checkouts/[unique ID 2]
OR
https://shop.mysite.ca/[unique ID]/checkouts/[unique ID
2]?step=contact_information

What I have so far, though not sure how to put the optional parameter "step=contact_information")

/^(https:\/\/shop.mysite.ca\/)([\da-z]+)(\/checkouts\/)([\da-z]+)$/

Upvotes: 0

Views: 51

Answers (3)

Abe
Abe

Reputation: 1415

Try it:

^(https:\/\/shop.mysite.ca\/)(\d+.)(\/checkouts)(\/)(\d+.)($|\?\w.+$)

If unique ID is composed with non numbers characteres:

^(https:\/\/shop.mysite.ca\/)([\da-zA-Z]+.)(\/checkouts)(\/)([\da-zA-Z]+.)($|\?\w.+$)

Upvotes: 0

sheplu
sheplu

Reputation: 2975

this should work

^(https:\/\/shop.mysite.ca\/)([\da-z]+)(\/checkouts\/)([\da-z]+)((\?step=contact_information)*)$

edit: forgot the ? and used * instead. The other solution by @thomas is a bit better I think

Upvotes: 0

Thomas Jager
Thomas Jager

Reputation: 5265

You can use a "?" to make a group either appear 0 or 1 times, making it optional.

/^(https:\/\/shop.mysite.ca\/)([\da-z]+)(\/checkouts\/)([\da-z]+)(\?step=contact_information)?$/

Upvotes: 1

Related Questions