Regex help - match one string but not another

Viewed 36

I have been using this:

~^\/student-accommodation\/(?:[^\/]+?)\/([^\/]+)\/$

to match for URLs like

/student-accommodation/manchester/ropemaker-court-manchester/

But now I need to edit this regex so it also matches for URLs like the below. All these new URLs will follow the same pattern and add a string that starts with #utm-source. Importantly they won't have another / in them.

/student-accommodation/manchester/ropemaker-court-manchester/#utm_source=afs&utm_medium=email&utm_campaign=ropemakercourt_afs_dec20

But then I don't want the regex to match for URLs like the below:

/student-accommodation/manchester/ropemaker-court-manchester/en-suite/

Can anyone help? I am a novice at regex! Thanks

1 Answers

Use

^\/student-accommodation\/[^\/]+\/([^\/]+)\/(?:#utm_source.*)?$

See proof

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  student-                  'student-accommodation'
  accommodation
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  [^\/]+                   any character except: '\/' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^\/]+                   any character except: '\/' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    #utm_source              '#utm_source'
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Related