regex - How can a regexp match a string followed by a char, or ending here? -


how can make regular expression match:

abc/123 abc/123/ abc/123/def 

but won't match:

abc/123def 

in other words, regexp match if string either:

  • ends here
  • continues /

a simple ^abc/123 match both of them.

how this:

^(.+\/)+\d+(\/.*)?$ 

explanation:

(.+\/)+  @ least 1 character, followed slash, @ least once \d+      digits (\/.*)?  optional slash, (also optional) 

this allow stuff like:

abc/123 abc/123/ abc/123/def abc/def/123/ghi/456 

Comments