Try like this:
pattern=somepattern
replacement=somereplacement
ext=ext
rename "s/$pattern.*?\.$ext$/$replacement.$ext/" *.$ext
The .*?
should match your "trashtext" in between the pattern and the extension, and replace it with the replacement. It doesn't matter if trashtext contains special characters.
If, as the replacement you want to use whatever was matched by the pattern, you can do like this:
rename "s/($pattern).*?\.$ext$/\$1.$ext/" *.$ext
Finally, it's best to use the -n
flag while playing with the syntax. This way the command will just show what it would do instead of really doing it:
rename -n "s/($pattern).*?\.$ext$/\$1.$ext/" *.$ext