Home > Net >  Regex syntax \(.*\) suppose to remove the ( ) and all characters between ( ). How it actually work
Regex syntax \(.*\) suppose to remove the ( ) and all characters between ( ). How it actually work

Time:06-15

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:

  1. Match exactly one opening parathesis
  2. Match an unlimited number of any character
  3. 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/\(*\)/}"

enter image description here

and

b='Test run1(2021-08-05-11-32-18_UTC).txt'
echo "${b/\(*\)/}"

enter image description here

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)
  • Related