Raz
Raz

Reputation: 11

Problem with basic regex to match ending optional character

Hi all i was hoping someone could help be with some basic regex i am really struggling with.

Bascially i need to match a url for redirection. I have been using

^~/abc(/)?

however i need to change the end part to just check the last optional character as this will also match ^/abcd

Upvotes: 1

Views: 2177

Answers (3)

hoang
hoang

Reputation: 1917

I'll do something like this : ^~/([a-zA-Z0-9]+/?)*$

Upvotes: 0

How about ^~/abc(/?)

or more generally: ^~/[a-zA-Z0-9]+/?

Upvotes: 3

Chris Tonkinson
Chris Tonkinson

Reputation: 14459

Assuming PCRE, you will want:

^~/abc(.)?$

Which will match "~/abc" followed (optionally) by any single character, which will be captured. Leave the () off if you don't need to capture said character.

Just like ^ matches the beginning of string (or line, depending upon mode), $ matches the end of string (or line).

Upvotes: 1

Related Questions