Subject: Re: [Name Mangler Talk] Find a known part of name followed by letters only

From: Rob Griffiths <[email-masked]>
Date: Sat, 13 Feb 2021 05:16:59 -0800

On Feb 13, 2021, at 1:03 AM, Jay Flaxman <[email-masked]> wrote:

> One other thing – is there a 'cheat sheet' kinda thing that you would recommend I refer to for writing expressions in Name Mangler? Does it use a language that is widely used elsewhere or is the language unique to Name Mangler?

From the help file, here it is in all it's geeky glory:

Name Mangler uses Apple's regular expression API <https://developer.apple.com/documentation/foundation/nsregularexpression <https://developer.apple.com/documentation/foundation/nsregularexpression>>. This API, in turn, uses the ICU pattern syntax <http://userguide.icu-project.org/strings/regexp <http://userguide.icu-project.org/strings/regexp>> for regular expressions. We have extended this syntax to include case conversion (e.g. \U) as in PCRE <http://www.pcre.org <http://www.pcre.org/>>.

What that all means in more normal language is that Name Mangler basically uses a standard regex engine, though there are some key exceptions: Unless disabled in prefs, any whitespace must be escaped (foo\ bar), and to replace captured groups, Apple's version of regex uses $1, $2, etc. instead of \1, \2, etc.

> As someone who is brand new to writing expressions it would be so useful to know what I need to write to achieve my desired result.

I wrote a primer about regex on my personal blog that might help; it links to sources that go into a ton more detail:

<https://robservatory.com/the-little-i-know-about-regexand-where-to-learn-more/ <https://robservatory.com/the-little-i-know-about-regexand-where-to-learn-more/>>

> For example, I had no idea that a 'd' would identify numbers

Technically, it's \d, which is shorthand for [0-9]. The backslash is important.

> or that a $ sign would refer to something that's in brackets in the previous input box. I've so far been guessing my way around a bit so a 'map' of some kind would be amazing! 🙂 

Yep, those brackets create variables on the fly—the first set in Find become $1 in Replace, the next $2, etc. They're called capture groups, and they're one of the most powerful parts about regex. Using them, you can easily rearrange filenames. Capture three groups in your Find, and you can replace them in whatever order you want—$2 $1 $3, etc.

-rob.

---

From: Jay Flaxman <[email-masked]>
Date: Sat, 13 Feb 2021 01:03:15 -0800 (PST)

That's fantastic, it works perfectly for what I need, thank you! Now I know 
how to identify varying numbers and letters I can just alter certain parts 
of that as I need to and apply it to other filenames so that's incredibly 
helpful!

My main problem was just trying to stop the hyphen from going between 'ABC' 
and the quantity as the quantity is part of the ABC description so I can't 
separate those two bits of information. To give a little more context, it's 
important where the hyphens in the filenames go as I later take the files 
into the Sketch app and replace the hyphens with forward slashes to 
organise all of the images into folders.

One other thing – is there a 'cheat sheet' kinda thing that you would 
recommend I refer to for writing expressions in Name Mangler? Does it use a 
language that is widely used elsewhere or is the language unique to Name 
Mangler? As someone who is brand new to writing expressions it would be so 
useful to know what I need to write to achieve my desired result. For 
example, I had no idea that a 'd' would identify numbers or that a $ sign 
would refer to something that's in brackets in the previous input box. I've 
so far been guessing my way around a bit so a 'map' of some kind would be 
amazing! 🙂 

Thanks again, much appreciated!
Jay
On Thursday, 11 February 2021 at 00:59:44 UTC Many Tricks wrote:

> On Feb 10, 2021, at 4:15 PM, Jay Flaxman <[email-masked]> wrote:
>
> > I need to keep 'ABC' and its quantity i.e. 'ABC 6x' with a space between 
> them and to add a hyphen between that and the rest of the filename (as I've 
> marked in red). The first part of the filename up to 'ABC' always stays the 
> same but the colour name, quantity – i.e. 6x, 10x – and the presence of the 
> quantity varies.
>
> Thanks - that definitely helps, but leads to (surprise!) more questions. 
> Do you need this solution to work for any possible filename, or just 
> filenames that start "bird-cup small-ABC?" Writing one that works for any 
> generic text is going to be tricky for (perhaps seemingly oddly) the 
> filename without the quantity if it's a generic formula you're after. Why 
> is that?
>
> The names with the quantities can be easily identified in a regular 
> expression—we're looking for any digits preceeding an 'x' that's followed 
> by a space. So I can find those and easily add the hyphen after the x. But 
> for the file *without* any quantity information, there's no way to target 
> where that hyphen needs to go, at least as far as I can tell. There's no 
> logical way to tell Name Mangler "put this hyphen after the word that 
> proceeded the '10x' in that other filename."
>
> If the solution needs to work for generic filenames, do those filenames 
> follow any sort of pattern I can identify? For instance, is it always three 
> letters after the last hyphen from the name part, which is then followed by 
> a space? Does the hyphen always come before the last two words? Anything at 
> all that looks like a pattern? Because if not, and it's truly random, then 
> you'll have to do some bit of manual labor to make it work for a given set 
> of filenames.
>
> Please download the attached Name Mangler config file, unzip it, then 
> double-click it. It will open in Name Mangler and show you one "partially 
> automatic" solution, as seen in the screenshot. The first step handles the 
> easier ones, those with the quantities.
>
> The second step tries to automate a match, based on what you provided—It 
> looked to me like "ABC" followed by a space followed by alpha characters 
> would work for this rule, so that's what I used. This won't match the other 
> files, because they have numbers after that space, so only the 
> quantity-free files are modified. But again, this is semi-manual, because 
> if "ABC" changes, this won't work.
>
> Does that help?
>
> -rob.
>
> ------------
>

---

From: Rob Griffiths <[email-masked]>
Date: Wed, 10 Feb 2021 16:59:39 -0800

On Feb 10, 2021, at 4:15 PM, Jay Flaxman <[email-masked]> wrote:

> I need to keep 'ABC' and its quantity i.e. 'ABC 6x' with a space between them and to add a hyphen between that and the rest of the filename (as I've marked in red). The first part of the filename up to 'ABC' always stays the same but the colour name, quantity – i.e. 6x, 10x – and the presence of the quantity varies.

Thanks - that definitely helps, but leads to (surprise!) more questions. Do you need this solution to work for any possible filename, or just filenames that start "bird-cup small-ABC?" Writing one that works for any generic text is going to be tricky for (perhaps seemingly oddly) the filename without the quantity if it's a generic formula you're after. Why is that?

The names with the quantities can be easily identified in a regular expression—we're looking for any digits preceeding an 'x' that's followed by a space. So I can find those and easily add the hyphen after the x. But for the file *without* any quantity information, there's no way to target where that hyphen needs to go, at least as far as I can tell. There's no logical way to tell Name Mangler "put this hyphen after the word that proceeded the '10x' in that other filename."

If the solution needs to work for generic filenames, do those filenames follow any sort of pattern I can identify? For instance, is it always three letters after the last hyphen from the name part, which is then followed by a space? Does the hyphen always come before the last two words? Anything at all that looks like a pattern? Because if not, and it's truly random, then you'll have to do some bit of manual labor to make it work for a given set of filenames.

Please download the attached Name Mangler config file, unzip it, then double-click it. It will open in Name Mangler and show you one "partially automatic" solution, as seen in the screenshot. The first step handles the easier ones, those with the quantities.

The second step tries to automate a match, based on what you provided—It looked to me like "ABC" followed by a space followed by alpha characters would work for this rule, so that's what I used. This won't match the other files, because they have numbers after that space, so only the quantity-free files are modified. But again, this is semi-manual, because if "ABC" changes, this won't work.

Does that help?

-rob.

------------

---

From: Jay Flaxman <[email-masked]>
Date: Wed, 10 Feb 2021 16:17:20 -0800 (PST)

Hi Rob,

Sorry about that, my examples are:

bird-cup small-ABC Blue.jpg
bird-cup small-ABC 6x Red.jpg
bird-cup small-ABC 10x Red.jpg

to

bird-cup small-ABC*-*Blue.jpg
bird-cup small-ABC 6x*-*Red.jpg
bird-cup small-ABC 10x*-*Red.jpg

I just need to add a hyphen in the right place within varying filenames 
basically! 🤯

I hope that makes more sense this time 🙂 

Thanks again,
Jay

On Sunday, 7 February 2021 at 16:06:57 UTC Many Tricks wrote:

> On Feb 5, 2021, at 4:21 PM, Jay Flaxman <[email-masked]> wrote:
>
> > For example, I currently have names e.g. "-ABC 10x-RestOfFilename" that 
> feature a numeric quantity that I want to keep as they are with a space 
> between but, if there's no numeric quantity, I just want to put a hyphen 
> and then keep the rest of the file name as it is e.g. "-ABC-RestOfFilename"
> > 
> > Find: ABC (followed by a space and letters only)
> > Replace: ABC-(followed by letters only)
>
> Jay:
>
> I'm pretty sure we can do this relatively easily, but what would make it 
> much simpler for me is a few "before and after" full filename examples for 
> both patterns of filenames. I'm not quite clear on what exactly the 
> "before" style of each looks like, and what you want the "after" part to 
> look like.
>
> thanks;
> -rob.

---

From: Rob Griffiths <[email-masked]>
Date: Sun, 7 Feb 2021 08:06:55 -0800

On Feb 5, 2021, at 4:21 PM, Jay Flaxman <[email-masked]> wrote:

> For example, I currently have names e.g. "-ABC 10x-RestOfFilename" that feature a numeric quantity that I want to keep as they are with a space between but, if there's no numeric quantity, I just want to put a hyphen and then keep the rest of the file name as it is e.g. "-ABC-RestOfFilename"
> 

Jay:

I'm pretty sure we can do this relatively easily, but what would make it much simpler for me is a few "before and after" full filename examples for both patterns of filenames. I'm not quite clear on what exactly the "before" style of each looks like, and what you want the "after" part to look like.

thanks;
-rob.

---

From: Jay Flaxman <[email-masked]>
Date: Fri, 5 Feb 2021 16:21:34 -0800 (PST)

Hi, I've spent a couple of hours searching for a solution to this but I 
must be using the wrong search terms or something. I've also tried the Help 
section of the app but again, could be looking in the wrong place.

Basically I'd like to find a part of the file name that I *know* followed 
by only letters that I *don't know* what they'll be other than falling 
between A-Z.

I've tried [A-Za-z] using Find and Replace with "Query is a regular 
expression" and "Ignore case" ticked, but it doesn't appear to work.

New to File Mangler today, but love it already!

I hope that makes sense, any help would be much appreciated!

Jay