Trouble with "Search and Replace Using Regular Expression"

Hey, Gang.

I'm trying to create a (seemingly) simple macro to take a phone number input as a string of numbers (1234567890) and convert it into a eyeball-friendly format (123-456-7890).

I've tried both the "Search Variable Using Regular Expression" and "Search and Replace" actions and neither seem to work at all.

What am I missing here?

convert phone number format Macro (v11.0)

convert phone number format.kmmacros (15 KB)

It works for me. Look at this: (look at the bottom of the image below, which contains the dashes that you want.)


I'm staring at your code and trying to figure out why it isn't working. In the meantime, just copy my action, validate that my action works for you, and then expand on it to match your way until it stops working.

EDIT: I think I see your problem. Your string "$1-$2-$3" (and the first expression, which is the one that matters) has extra carriage returns attached to it that are causing the match to fail. Technically, that's your fault, but I think it illustrates a design problem in this KM action. At the least, this KM action should generate a yellow triangle alert if the user has CR's in this action.

Yeah, definitely not working:

Very strange.

I'm just going to restart my computer and see if that magically solves it.

I just gave you the answer above. Don't restart! Reread my last message.

Aha! There was indeed an extra carriage return, but in the regex string. I never would've figure that out. Thanks, @airy!

That's an example of a problem that cannot be fixed by visual inspection. That's why I nearly burned my eyes starting at your problem, and was forced to run your code instead. Also, the hidden CR's were in BOTH fields.

There are several KM actions which contain the ability to store multiple lines of data that the user can't see. I've actually exploited this "bug" for useful purposes, but perhaps it can also be exploited for malicious purposes. That's why I suggested a yellow triangle alert be added to this macro when the data contains additional lines.

1 Like

I think I spotted another bug in your code. Let me test it. You won't believe it when I tell you.

Wow. I'm 99% sure you will agree I spotted a bug. But this deserves its own thread. I'm going to post a new thread, because it's a different topic. Keep an eye open for a new thread about semaphores.

Regarding your "curiously" comment, (<-->IN THE COMMENT OF YOURS BELOW) I can't figure that one out because I can't see the macro. As this case proves, it's hard enough spotting errors even when you can see the macro, but it's twice as hard when you can't see it. :slight_smile:

This one works now.

convert phone number format Macro (v11.0)

convert phone number format.kmmacros (15 KB)

Curiously, an identical action to the search and replace had also previously failed and continued to fail even after I retyped the regex and replacement strings.

I'm curious to find out what that other bug was, @Airy. I totally believe I won't believe it.

I was confusing %ExecutingInstance% with %ExecutingThisMacroUUID%, so there is no bug. My mistake. You are using it correctly. I was conflating these two tokens. So there will be no new post. I'm a tad embarrassed, but I'll survive.

No worries, Brother! :smiling_face:

The trouble with Regular Expressions is that, in reality, they just waste hours of our lives in debugging.

( The best way to shorten the debugging time is always to throw the regular expression away :slight_smile: )

A regular expression is like the kind of fish for which the best recipe is always:

  • Place fish on well-buttered wooden board and bake for 2 hours, then
  • discard the the fish and eat the board.

Chunked telephone number (without debugging regular expressions)

Chunked telephone number (without debugging regular expressions).kmmacros (4.7 KB)

Point taken. Thank you. :pray:

1 Like