Wildcard Help with regex

Post any Bulk Rename Utility support requirements here. Open to all registered users.

Wildcard Help with regex

Postby cyberlunacy » Fri Jul 01, 2022 1:54 pm

I would love to learn and understand the wildcard expressions used in other threads but i see no correlation between what people are told to run and how it translates to being able to use it.
I am no programmer but once i understand the use case i can utilize it.

So here is my dilemma.

files have this at the end and i want them removed [120897fgha985].fileextension

so it could be this filename[ajfh1845675].xyz
i want anything inside the open/close brackets, along with the brackets removed so its just filename.xyz and i just don't see how to do that under the RegEx area.

Thanks!!
cyberlunacy
 
Posts: 2
Joined: Tue Jun 28, 2022 8:52 pm

Re: Wildcard Help with regex

Postby therube » Fri Jul 01, 2022 5:00 pm

General idea, is to think of how items can be related to as a group.

One type of group is "everything"; .*
Basically says match everything. And that is fine, but you then want to filter something out.

(.*) match everything
(.*?) match everything, but don't be greedy (the ?)
(.*?)[.*] match everything, but don't be greedy, but only up to a [ followed by anything up to the last ]

(.*?) is referenced in the Replace: as \1
[.*] is not "grouped" (because it was not put in parenthesis), so will not end up in the output name, i.e., that part will be cut from the name

And that may be sufficient for your needs.

RegEx:
Code: Select all
Match:  (.*?)[.*]
Replace:  \1


That's the basic premise, but there are always gotchas & nuances.

"file name 1 [gobbly] [gook].txt"
"file name 1 [random.txt"
"file name 1 [random].txt"

What will the above code do with those particular examples? Well...

So depending on what you have, & what you're looking to get, you may need to tweak even more.


RegEx:
Code: Select all
Match:  (.*?)([.*])
Replace:  \2 - \1

Now I've placed the [.*] in parens, so I can now reference it in the Replace: (& added a dash).
And in this example, I've switched positions 1->2 to 2->1.

So...

"file name 1 [random].txt"
becomes
"[random] - file name 1.txt"


(I'm not very good at this, but it should give some insight.
And there are varying RegEx versions (& syntax) that may be easier to code, depending...)
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Wildcard Help with regex

Postby Luuk » Sat Jul 02, 2022 12:24 am

This just a few examples, because already there is a very good starting post at https://www.bulkrenameutility.co.uk/forum/./viewtopic.php?f=3&t=96

Regex wildcard =======> How many: of which characters to match
.* ==================> Zero-or-more: Any character
\d+ =================> One-or-more: Any digit 0-9
\w{9,12} =============> From 9-12:::: Any word-character

[A-Za-z\d]{1,4} ======> From 1-4:::::: Any uppercase english letter, any lowercase english letter, or digit 0-9
[^a-d]+ ============> One-or-more:: Any character that is not a thru d
[a-d]+ ==============> One-or-more:: a, b, c or d
[abcd] ==============> One of either: a, b, c or d
[a-d] ===============> One of either: a, b, c or d
[.*] ================> One of either:: . or * (but * cannot be inside of windows filenames)

Since the brackets say which [characters] to match, we use \[ to match any [ and also \] to match any ] inside of the filenames.
So to match [xyz] at the end of a filename, we say it like... \[xyz\]$

The main thing to remember in the beginning, is that we must match, and also (group) any parts that we want to keep!
So unless putting a checkmark inside of "v2", the Match and Replace to remove [xyz] from the ends, can look like...
(.+?)\[xyz\]$
\1

But since your filenames can have many variations inside of [here], I would probably say it more like...
(.+?) *\[[^][]*\]$
\1
The [^][]* is saying Zero-or-more: Any characters that are not ] or [, so renaming like...
Filename [text]-some-more-text [abcdefg].txt ====> Filename [text]-some-more-text.txt


You could say it much simpler with something like...
(.+?) *\[.*\]$
\1
But since .* is saying Zero-or-more: Any character, this could rename like...
Filename [text]-some-more-text [abcdefg].txt ====> Filename.txt

Its very easy making a mistake like [.*] thinking to match anything inside of the brackets, Im often do this myself.
But experimenting inside of the "Match" while looking at the "New Name" column, can often help to discover many corrections.
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: Wildcard Help with regex

Postby cyberlunacy » Tue Jul 12, 2022 9:46 pm

Luuk wrote:This just a few examples, because already there is a very good starting post at https://www.bulkrenameutility.co.uk/forum/./viewtopic.php?f=3&t=96

Regex wildcard =======> How many: of which characters to match
.* ==================> Zero-or-more: Any character
\d+ =================> One-or-more: Any digit 0-9
\w{9,12} =============> From 9-12:::: Any word-character

[A-Za-z\d]{1,4} ======> From 1-4:::::: Any uppercase english letter, any lowercase english letter, or digit 0-9
[^a-d]+ ============> One-or-more:: Any character that is not a thru d
[a-d]+ ==============> One-or-more:: a, b, c or d
[abcd] ==============> One of either: a, b, c or d
[a-d] ===============> One of either: a, b, c or d
[.*] ================> One of either:: . or * (but * cannot be inside of windows filenames)

Since the brackets say which [characters] to match, we use \[ to match any [ and also \] to match any ] inside of the filenames.
So to match [xyz] at the end of a filename, we say it like... \[xyz\]$

The main thing to remember in the beginning, is that we must match, and also (group) any parts that we want to keep!
So unless putting a checkmark inside of "v2", the Match and Replace to remove [xyz] from the ends, can look like...
(.+?)\[xyz\]$
\1

But since your filenames can have many variations inside of [here], I would probably say it more like...
(.+?) *\[[^][]*\]$
\1
The [^][]* is saying Zero-or-more: Any characters that are not ] or [, so renaming like...
Filename [text]-some-more-text [abcdefg].txt ====> Filename [text]-some-more-text.txt


You could say it much simpler with something like...
(.+?) *\[.*\]$
\1
But since .* is saying Zero-or-more: Any character, this could rename like...
Filename [text]-some-more-text [abcdefg].txt ====> Filename.txt

Its very easy making a mistake like [.*] thinking to match anything inside of the brackets, Im often do this myself.
But experimenting inside of the "Match" while looking at the "New Name" column, can often help to discover many corrections.


Wow i gotta say thanks for all of that, it was more than i could have expected and more than i undersand.
what you gave me works perfectly
(.+?) *\[[^][]*\]$
\1
i still cant read it and fully understand it, but I will save this to use in the future, the more i use it the more familiar i will become with it.
atleast with what you gave me i can change params and remove chunks i want to, hopefully :)
cyberlunacy
 
Posts: 2
Joined: Tue Jun 28, 2022 8:52 pm


Return to BRU Support