Delete portion of variable?

I want to delete a portion of a variable and update the variable:

Sample string:
Ring - 8mm - Flat - 10.5 - Titanium - FingerPrint
Updated String:
Ring - 8mm - Flat - Titanium - FingerPrint

This is what I tried, but it's not updating the variable. What am I doing wrong?

Mutation, mutatis mutandis, is always confusing.

Easier, I find, to think in terms of producing an additional derived variable (pruned), rather than delete-mutating the unfortunate existing variable (in place) – that is a quickish route to mishap and puzzlement.

(You can always clear all temporary variables at the end)

sample

1 Like

Hey Ray,

Please always provide a working sample macro – this will greatly facilitate anyone who wants to help you – and make it much more likely that someone will.

Although it is sometimes possible to tell on cursory examination what is wrong with a macro, it is generally necessary to actually test it in action to properly debug it.

It took me 5+ minutes to recreate your macro, and I can't be certain I got all the fiddly bits of the regular expression correct.

Nevertheless – using your sample string I got a correct answer the first time – even though your regex is a bit off:

A) You don't have to escape spaces in a regular expression.

B) (<your_pattern>)+ shouldn't be necessary.
	– I.E. the parentheses and the outer + sign
	– What were you trying to accomplish?

Do you have BBEdit? It makes creating and debugging regular expressions easier than fooling with them directly in Keyboard Maestro.

(Even the free version is much more powerful than TextWrangler used to be.)

You can use the Live Search function to create a regular expression live and see it work – much as you can with the Regex101.com site – but without all the distractions. (I also use an app from the app-store called Patterns for this – and an app called RegExRx – but I always start with BBEdit.)

Now then – you're aware that when you use Local variables they are DEAD as soon as the macro completes?  (This can unexpectedly affect actions that display action results inline.)

If you want to see local variable contents you need to use a Display Text action or another mechanism.

I usually don't develop macros using local variables, because it makes debugging more difficult. I only change variables to local after I've got a macro working.

I'm nearly always sorry if/when I break that rule...  :sunglasses:

-Chris

1 Like

Thank you for the pointers, Chris. Will do.

Knowing when to use Local Variables is confusing to me. Thanks for the pointers.

Yes, I recently downloaded BBEdit. Using it intelligently is another story. I'll try the "Live Search" function.

@ComplexPoint - I had to at least try a RegEx on my own. All of you make it look so simple (but it's not). There seem to be so many approaches. Thanks for the great solution!

As Chris says, it is always best to provide a compete example of real-world before/after data.
But, using this limited example. . .

It should be very easy, based on the face of the differences, using only one Action: Search and Replace action
No RegEx needed.

Normal String Search on the Original Variable:
Flat - 10.5 - Titanium

Replace "Source" (all occurrences):
Flat - Titanium

But in case you other similar lines in the same Variable you don't want to change.
You don't need the IF/THEN. If the search fails, nothing is changed.

RegEx Search:
(Ring.+)10.5 \- (.+)

Replace:
\1\2

For details, see:

My first rule: Keep it Simple

You're right, I need to provide more info.

We potentially have 100's of "ring" product names because of multiple sizes. Since the size is irrelevant to the template choice, it is necessary to strip it out of the variable. RegEx does that nicely. The "IF/THEN" action is to handle "Ring" products separately from other products which will be used as they are captured.

I really think I have a working solution now. Moving it from a "Switch" action to searching an external .txt file make a WORLD of difference in editing.

One more question: I'm maintaining the Text file in Google Sheets. When I export the page it saves in ".TSV" format. I was initially changing the extension to ".TXT" which works. I've since realized KM recognizes the "TSV" as well. Is there any reason not to just access it as a ".TSV" file in KM?

Perhaps this should be posted as a new question.

If you need the IF/THEN, then use it.
My point was that the RegEx search includes a qualifier for "Ring". If a match is not found, then no changes are made.

Well, you tell us! LOL
Does KM work with the .tsv file?
I would guess so, since it is still just a text file.