Regexp and back reference

There’s some difference between Java’s regexp plus back reference and the GAWK regexp methods.

I would like to, for example, cut the pure filename out of a string: no path and no ending. (I know the File class and its handy methods but it’s for strings only and a general question).
Of course with several lines of code it’s well possible. :slight_smile:

However is it also possible with the String.replaceFirst(regexp, replace) method or a one-liner with Pattern.compile(“regexp”).matcher(string).etc ?

With GAWK and its powerful gensub I’d do something like this:
$0 = “c:/Path/Filename.png”;
gensub(/.+\(.+).png$/, “\1”, “g”)
in order to get “Filename”.

In Java I couldn’t figure out how to use the back reference in the replacement argument. Is it just for the regexp argument?

I’m just asking because I’m used to the one-liner of GAWK.

Well, then two replaceFirst in one line… Unlike AWK we’re OO.

thestring.replaceFirst(".+/", “”).replaceFirst("\.png", “”)