[Solved] What will be the regex for the given case? [closed]


You want that $ in either case. Instead of ‘slash OR end’, it’s more ‘optional slash and then a very much not-optional end’. So.. /?$. You don’t need to normally escape slashes, especially in java regexes:

Pattern.compile("[-/](\\d+)/?$")

This reads: From minus or slash, a bunch of digits, then 0 or 1 slashes, then the end. Note, use find() and not matches() – matches only works if the entire string matches, which it won’t, as the – or / occurs halfway through.

EDIT: Was missing a backslash in the java string.

1

solved What will be the regex for the given case? [closed]