Home > database >  How to select a part of a string in a set, but not the string exactly in RegEx
How to select a part of a string in a set, but not the string exactly in RegEx

Time:04-16

I am trying to select part of a URL /example/privacy-policy and /example/123/privacy-policy but not /privacy-policy

I currently have this ^[^\/privacy\-policy].\/privacy\-policy.$

But it seems to not work still. Ideally, it would be able to find privacy-policy anywhere in the string without directly matching the root /privacy-policy

Thank you very much!

CodePudding user response:

If you don't want privacy-policy as the root of your url, but you still want to match it, you can force the regex to look for at least one more symbol before the backslash that preceeds privacy-policy:

(. \/)privacy-policy

Then if you want to get the part that comes before privacy-policy, you can reference Group 1.

Is this what you're looking for?

CodePudding user response:

^.*(?=\/privacy-policy)

This matches everything (.*) from the beginning of the string (^) up until the string /privacy-policy appears. (This is called a positive lookahead)

  • Related