i need help with a REGEX expression (for analytics). not sure how to handle the requirements. here's an example of a URL: /a.html?ref=aa&project=11&utm=bb
this URL ould have &project=XX in the middle but it is possible that &project won't be there at all..
requirements -
- i want the regex to be positive only for specific project=XX (for example only when XX equals 11 or 12 or 13) but negative for all other values (project=22).
- the parameter before it (?ref in the example below) is mandatory
- any parameter afterwards (&utm) is optional.
for example -
- fine: /a.html?ref=aa&project=11&utm=bb
- fine: /a.html?ref=aa&utm=bb
- not fine: /a.html?ref=aa&project=22&utm=bb
how do i approach this?
i tried this it kinda works (but only without additional utm params):
/a.html?ref=aa(&project=(11|12|13))?$
i tried this, but it doesn't work when using the utm paramater :
/a.html?ref=aa(&project=(11|12|13))?(&utm=.*)?$
thanks Itay
CodePudding user response:
You don't say what platform you're using, but you'll need to escape your forward slashes and question marks if you want them to match literal characters on most platforms:
\/a.html\?ref=aa(&project=(11|12|13))?(&utm=.*)?$
You might also want to minimize your capture in the utm
block in case other things come after it that you don't want:
\/a.html\?ref=aa(&project=(11|12|13))?(&utm=.*?)?$
CodePudding user response:
You could use character class [123]
to match either 1,2 or 3 with a single optional group, and note to escape the dot to match it literally.
\/a\.html\?ref=aa(&project=1[123])?&utm=.*$
The pattern matches:
\/a\.html
match/a.html
\?ref=aa
Match?ref=aa
(
Capture group&project=1[123]
Match &project=1 and then either 1,2 or 3
)?
Close the non capture group to make it optional&utm=.*$
Match &utm= followed by the rest of the line