Regex else-if help

Hello!

I am hoping to create an action that determines if a variable is 10 digits or more than 10 digits then will execute one action or another based on that factor. Any help is greatly appreciated!

There will be many ways to do this. My way is probably not the best or easiest to understand. But my way lets you do it in one action.

Basically it counts the number of digits by piping the Variable through a sed filter in Unix removing anything that isn't a digit then using the wc utility to count how many characters are left. Then it compares the result to 10 and branches as you wish.

It's the way I do things, but if you want a different way, or simpler way, you can ask.

P.S> I didn't test it! I'll be embarrassed if there's a typo.

P.S> Technically you don't need the sed component if we can assume the variable already contains digits.

P.S> another minor variation that might be clearer is to replace the line of text above with this line:
grep -o "[[:digit:]]" | wc -l

P.S. Yet another solution might be this:

I would consider the above solution to be humorous because it calculates the number of digits using a binary calculator.

To be most helpful, we need real-world examples of your source data.
For now, I will assume the digits of interest could be anywhere in the source string. The RegEx will only look for the first set of digits.

Here is an example macro to show you the technique. You will need to adapt for your specific needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Branch (IF/THEN) Based on Number of Digits [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/e/d/ed29faf425a04e906f0dc4c6688c0d3b9e0e015d.kmmacros">Branch (IF-THEN) Based on Number of Digits [Example].kmmacros</a> (9.9 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Author.@JMichaelTX 

**PURPOSE:**

* **Branch Based on Number of Digits in String**

**HOW TO USE**

1. First, make sure you have followed instructions in the _Macro Setup_ below.
2. Trigger this macro.

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.
1. Assign a Trigger to this maro.
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * SET to Your Source Text 

**REQUIRES:**

1. **KM 8.2+**
2. **macOS 10.11.6 (El Capitan)**

TAGS:  @RegEx @Strings

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


**==USE AT YOUR OWN RISK==**

* While I have given this a modest amout of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

![image|555x1329](upload://68hOoJjAbExteQxNYRBs68aiLeg.png)

In the interest of alternative solutions, here's what came to my mind to solve this problem:

Switch:Case Based on Number of Digits.kmmacros (3.0 KB)
image

I wanted to see if I could find another solution. All solutions do more or less the same thing but this one uses AppleScript:

This solution makes no attempt to filter out non-numeric characters. It was never clear if that was a requirement of the original question.

By the way my knowledge of AppleScript is so lame I don't think I'm even qualified to properly explain how this works. I just poked around until it worked.

Here's a new solution that should work but it doesn't quite work, apparently because KM appears to have a limit of about 9.9 decimal digits of accuracy.

The problem is that in KM, LOG10(999999988) gives 8.99999999 and LOG10(999999989) gives 9.0. Of course neither answer is correct, which isn't the problem, because they are rounded answers but the rounding is just slightly to weak to allow this solution to work here. Otherwise it's a pretty neat trick. It doesn't require using any sort of string LENGTH function.

I tried googling what the mathematical precision of KM calculations really is, but couldn't find an answer. Perhaps one of the KM wizards here can answer this. Based on my calculation above, it appears to be just shy of ten digits of precision.

You guys are amazing! I'm sure I'll be able to find a solution from your responses when I have a moment. If I have any questions along the way I know where to look. Thanks again!

1 Like

This is not technically accurate.

Keyboard Maestro uses “doubles” for calculations, which has about 15 significant figures of accuracy.

However, numeric results only include 8 decimal places of answer, so anything smaller than that will be rounded.

But you can, for example, increase the value to get more precision:

The precision there is about 15 digits (up to the 0). The digits after that are imprecise.

So accurate would be to say Keyboard Maestro has roughly 15 significant figures and up to 8 decimal digits.

Thanks gglick! I think this one is on the right track for me. However I actually probably worded my original question wrong in the first place. I need this regex to count the number of 'characters' not just ' 'digits' because there will be a mix of letters and numbers. Is that possible?

You need to provide us with some real-world examples of your source text, so that we can provide better answers and don't waste anyone's time.

How would you manually identify the start and end of the string of numbers and letters to be counted?

Please read:
Tip: How Do I Get The Best Answer in the Shortest Time?

It's definitely possible, but if you want to count a mixture of characters including but not limited to digits, and the variable you will be using only contains the text you want to count, I would go with either a Character Count Filter based macro like @JMichaelTX showed you, or one like this that takes @Sleepy's idea and modifies it to use a different function explicitly for counting the number of characters in a string:

Switch:Case Based on Number of Characters.kmmacros (2.6 KB)
image

If this doesn't resolve your question though, I second @JMichaelTX's advice in his last post and encourage you to read the link he shared and provide more details about your workflow and goal.

Thanks Gabe! This solution worked amazingly. :100:

You guys rock!

1 Like