This should do it:
sub(' *\\([^)]*)$', '', "n-Pentacosane (C-25)")
That is:
sub
is enough, because we will do a single replacement- To match the starting bracket we need to write:
\\(
. No need to escape the second. - To match at the end, we need two things:
- match at the end using
$
- make the matching non-greedy: by using
[^)]*
we avoid replacing too much in input likepenta (something) cosane (C-25)
, to getpenta (something) cosane
as result instead ofpenta
- match at the end using