Replacing Text between []

A swapping-ground for Regular Expression syntax

Replacing Text between []

Postby MrGadget » Tue Feb 19, 2019 3:55 pm

I've been trying to learn some simple Regex over at regex101.com. My goal was to learn how to replace the characters between a set of braces using regex (i know it can be done other ways, but I am trying to learn some regex). The folder names all end with with a value between braces ([MP3 - 320K], [FLAC], [MP3 - 256K], etc.). I want to replace them all with [MP3 - 192K]. At regex101.com , match: \[.*?] ; substitute: [MP3 - 192K] works, but not using BRU. With BRU I end up with [MP3 - 192K]. I've tried to follow and modify other examples, but have not been successful - too much of a noob at this point. I would appreciate any help.
MrGadget
 
Posts: 4
Joined: Tue Feb 19, 2019 3:33 pm

Re: Replacing Text between []

Postby Panchdara » Wed Feb 20, 2019 1:05 pm

Specifics? Can you supply a couple specific examples of what you want? ie "name of file before" "name of file after".
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm

Re: Replacing Text between []

Postby MrGadget » Wed Feb 20, 2019 5:09 pm

Gladly...

Before:
Florida Georgia Line - Can't Say I Ain't Country (2019) [MP3 - 320K]
Hayes Carll - What it Is (2019) [FLAC]
Luke Combs - This One's for You Too {DE} (2018) [MP3 - 256K]
Sundance Head - Stained Glass And Neon (2019) [MP4 - 256K]

After:
Florida Georgia Line - Can't Say I Ain't Country (2019) [MP3 - 192K]
Hayes Carll - What it Is (2019) [MP3 - 192K]
Luke Combs - This One's for You Too {DE} (2018) [MP3 - 192K]
Sundance Head - Stained Glass And Neon (2019) [MP3 - 192K]
MrGadget
 
Posts: 4
Joined: Tue Feb 19, 2019 3:33 pm

Re: Replacing Text between []

Postby Panchdara » Wed Feb 20, 2019 6:49 pm

Ah, so change all the nnnK within the [ ] to 192K?

Try this
Code: Select all
RegEx (1)
Match: (.*)\[(.*) - (.*)
Replace: \1 [\2 - 192K]


Works on your files not sure if there are any deviations
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm

Re: Replacing Text between []

Postby MrGadget » Wed Feb 20, 2019 9:49 pm

Not exactly, as in some of the original file names the codec names are not MP3 (MP4, FLAC, etc.), but the files after should all be [MP3 - 192K], irrespective of what was between the [ ] in the original file names. So the goal is to replace what ever is between the [ ] with MP3 - 192K, or capture [xyz- abc] and replace with [MP3 - 192K]
MrGadget
 
Posts: 4
Joined: Tue Feb 19, 2019 3:33 pm

Re: Replacing Text between []

Postby Panchdara » Wed Feb 20, 2019 10:10 pm

Code: Select all
RegEx (1)
Match: (.*)\[
Replace: \1 [MP3 - 192K]


edit: this will leave 2 spaces between the name and the [MP3 - 192K], so add
Code: Select all
Remove (5)
check mark D/S  /* Which will remove double spaces */
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm

Re: Replacing Text between []

Postby MrGadget » Wed Feb 20, 2019 11:01 pm

Thanks you very much for the assistance. Now I will study your solution and try to learn from it so I am able to apply it to other efforts in Regex land.
MrGadget
 
Posts: 4
Joined: Tue Feb 19, 2019 3:33 pm

Re: Replacing Text between []

Postby Panchdara » Thu Feb 21, 2019 9:02 am

For more info, see first post in this section also,https://www.princeton.edu/~mlovett/reference/Regular-Expressions.pdf
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm


Return to Regular Expressions