I am new to Regex world. I would like to rename the files that have time stamp added on the end of the file name. Basically remove last 25 characters before the extension.
Examples of file names to rename:
IMG523314(2021-12-05-14-51-25_UTC).jpg > IMG523314.jpg
Test run1(2021-08-05-11-32-18_UTC).txt > Test run1.txt
To remove 25 characters before the .extension (2021-12-05-14-51-25_UTC) or if you like, remove the brackets ( ) which are always there and everything inside the brackets. After the right bracket is always a dot '. "
Will Regex syntax as shown in the Tittle here, select the above? If yes, I wonder how it actually works?
Many Thanks, Dan
CodePudding user response:
Yes \(.*\)
will select the paranthesis and anything inside of them.
Assuming when you ask how it works you mean why do the symbols work how they do, heres a breakdown:
\(
& \)
: Paranthesis are special characters in regex, they signify groups, so in order to match them properly, you need to escape them with backslashes.
.
: Periods are wildcard matcher, meaning they match any single character.
*
: Asterisks are a quantifier, meaning match zero to inifite number of the previous matcher.
So to put everything together you have:
- Match exactly one opening parathesis
- Match an unlimited number of any character
- Match exactly one closing bracket
Because of that closing bracket requirement, you put a limit to the infinite matching of the asterisk and therefore only grab the parenthesis and characters inside of them.
CodePudding user response:
Yes, it's possible:
a='IMG523314(2021-12-05-14-51-25_UTC).jpg'
echo "${a/\(*\)/}"
and
b='Test run1(2021-08-05-11-32-18_UTC).txt'
echo "${b/\(*\)/}"
Explanation:
- the first item is the variable
- the second is the content to be replaced
\(*\)
, that is, anything inside paranthesis - the third is the string we intend to replace the former with (it's empty string in this case)