-2

I have a URL and I'd like to match the ending of it exactly.

The url is [some_host]/url/action If the URL contains "url/action" in it I'd like for it to be matched.

Additional Rule 1: The URL can contain more data on the end e.g. "[some_host]/url/action/newTransaction/" -- in this case I'd like for the regex to not match it.

Additional Rule 2: However if the URL is "[some_host]/url/action?t=true then I'd like for the regex to match.

What should my regex look like?

EDIT: Some examples:

"[some_host]/url/action" -- should match. 
"[some_host]/url/action/" -- should match.
"[some_host]/url/action?t=true" -- should match 
"[some_host]/url/action/?t=true" -- should match 



 "[some_host]/url/action/[ANYTHING_ELSE]" -- should NOT match
Allen S
  • 3,133
  • 4
  • 27
  • 45
  • so basically you want to match exactly for 'url' with three `/` ? – Bagus Tesa Dec 21 '16 at 02:09
  • No. I've added some examples into the question – Allen S Dec 21 '16 at 02:09
  • does `[some_host]` always http or https? also, does the second part of the url always url followed by action or its already a template (meaning, catch all, as long as valid for url -- ie. `http://somewhere.com/some_url/some_action?parameter=1`)? – Bagus Tesa Dec 21 '16 at 02:17
  • It will always be http or https. I would like to give the regex lots of URL's and it should only catch URL's that have the string "url/action" in it and in addition also match the examples I have added – Allen S Dec 21 '16 at 02:22
  • 1
    Do you want this? https://regex101.com/r/Qsz19N/1 – Mr.kang Dec 21 '16 at 02:31
  • does `?t=true` also to be caught, is there any other variation? or its just always having `url/action` and optionally have parameter `t=true`? – Bagus Tesa Dec 21 '16 at 02:34

2 Answers2

0

This works your case:

(http|https):\/\/[\w_-]+(?:(?:\.[\w_-]+)+)\/url\/action(?!\/\w)[\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-]*

Use the full match in the above regex.

https://regex101.com/r/eGCYf6/2

Duc Filan
  • 5,158
  • 1
  • 18
  • 24
0

This is not a regex, is verbose. BUT, is easy to understand.

string url = "";
bool b1 = url.EndsWith("/url/action");
bool b2 = url.EndsWith("/url/action/");
bool b3 = url.EndsWith("/url/action/?t=true");
bool b4 = url.EndsWith("/url/action/?t=false");
bool match = b1 || b2 || b3 || b4;
Phillip Ngan
  • 12,647
  • 5
  • 61
  • 70