Johnny C.
Johnny C.

Reputation: 1

Regex matching string that doesn't end with any of multiple options

I'm a complete neophyte at this, but I'm essentially trying to write a regex to redirect a website automatically.

I'm trying to get imgur to always redirect to grid view, and I got stuck.

So far, I have got ^https?://imgur\.com/a/((?!.*/layout/grid$).*) redirecting to https://imgur.com/a/$1/layout/grid. This was the only way I could get it to not get stuck and add multiple lines to the end of the url, by specifying that the string must end with /layout/grid/.

I'm essentially stuck in that sometimes, the album url has some extraneous stuff at the end, which needs to be gotten rid off. For example, https://imgur.com/a/L7D9i#0. I need to get rid of the # and everything after it before I can add on the /layout/grid/, or it doesn't work. I tried to split it into two, but I can't figure out how to get the expression to match a string that doesn't end with either layout/grid or #\d. This is as far as I got, but it doesn't work: ^https?://imgur\.com/a/(?!(/layout/grid$|.*#\d$).*)

Sorry if this is ridiculously basic or something, I don't actually know regex, and everything I managed was from google, so I don't even well understand the syntax I'm using.

Upvotes: 0

Views: 90

Answers (1)

RamenChef
RamenChef

Reputation: 5558

Your expression is written differently from when you just had the /layout/grid. By which I mean, a parentheses is shifted and a .* is missing. The proper expression would be this:

^https?://imgur\.com/a((?!.*/layout/grid$).*)(?=#)

The original expression doesn't even appear to try to shave it off; it causes the match to fail if it has the #. Although, if that was what you wanted, use this:

https?://imgur\.com/a((?!(.*/layout/grid$|.*#)).*)

Upvotes: 1

Related Questions