0

Using Regex, I want to match any URL that includes the /it-jobs/ but must have something after the final /.

To be a match the URL must have /it-jobs/ + characters after the trailing / otherwise it should not match. Please refer to below example.

Example: www.website.com/it-jobs/ - is not a match
www.website.com/it-jobs/java-developer - is a match
www.website.com/it-jobs/php - is a match
www.website.com/it-jobs/angular-developer - is a match

  • 1
    What about `GET` parameters? Would `.../it-jobs/?key=value` be a match? – Rogue Aug 18 '22 at 13:48
  • @Rogue that scenario is not possible in the way this was developed, but as per regex yes, that would be a matching case. – Paulo Brás Aug 19 '22 at 08:26
  • 1
    If you need a concrete answer please add **all** pattern requirements in a verbal form in the question. We cannot help until you come up with what you need. I suggested `/it-jobs/[^/\s]+$` that only matches `/it-jobs/` and then *any one or more chars **other than** ``/`` **and whitespaces***. If that is what you need add these requirements to the question. – Wiktor Stribiżew Aug 19 '22 at 11:16
  • @WiktorStribiżew I tried to provide more clarity, but I don't think I can't be more specific. I do not need ```/it-jobs/``` to match as in the example provided. – Paulo Brás Aug 22 '22 at 09:24
  • And what I suggested - https://regex101.com/r/zyKQrL/3 - does not match `/it-jobs/`. It was downvoted, so considered not helpful. No idea what will be until you explain it clearer than " I do not need `/it-jobs/` to match". Best way is to describe what you want to match, this is what regex is for. – Wiktor Stribiżew Aug 22 '22 at 10:48
  • @WiktorStribiżew but i described it - to be a match it must contain ```/it-jobs/``` + characters after the trailing ```/```. What is not clear? I even provided examples of what should be a match and not a match. – Paulo Brás Aug 22 '22 at 10:52
  • @WiktorStribiżew your regex works :) but just one more question. In order to match the text before the first ```/``` do we need to add anything to your regex, so that for example the all ```www.website.com/it-jobs/java-developer``` is a match? – Paulo Brás Aug 22 '22 at 11:01
  • 1
    Add `.*` to match any text. I undeleted my answer so that we can finalize the question. – Wiktor Stribiżew Aug 22 '22 at 11:37

1 Answers1

0

You can use

/it-jobs/[^/\s]+$

To match the whole string, add .* at the pattern start:

.*/it-jobs/[^/\s]+$

See the regex demo.

Details:

  • .* - zero or more chars other than line break chars as many as possible
  • /it-jobs/ - a literal string
  • [^/\s]+ - any one or more chars other than / and whitespaces
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • @WiktorStribiżew there shouldn't be any whitespaces after "/". My point here is that it must have characters after the "/it-jobs/" path. – Paulo Brás Aug 19 '22 at 08:25
  • @PauloBrás My regex does not allow any whitespaces after `/it-jobs/`, why are you even mentioning it? Did you test it? – Wiktor Stribiżew Aug 19 '22 at 11:13