How Do I Extract Numbers from Text using Regex?

I have problem, need resolution, please

My macro searching for numbers in different texts. The second picture shows the result. Need the result as pictured 3.
Next step
Open new tab on Chrome past one number and search, next open new tab past second numbers and next …
All the searchable strings of numbers opened in the browser on new tabs picture 4

Remove Blank Lines (Zero or more white space) 1.kmmacros (3.2 KB)


1 Like

There's almost certainly a better way to do this, but here's one way to isolate those 10 digit numbers:

Isolate 10-Digit Numbers.kmmacros (4.2 KB)

1 Like

Work great, thx, but how past this numbers to web page chrome, as in the last picture ?

That's the thing about RegEx: There is usually 1001 ways to write it. LOL
Of course, each author may think his/her solution is best, but "best" is in the eye of the end user. :smile:

I think you have a great start, and thanks for putting your solution out there.

My only thought is that it would allow capture of 10 digits of numbers > 10 digits. While the OP stated that he wanted only numbers with 10 digits, he did not stated whether or not the text might contain larger numbers.

So here is another solution that restricts the match to exactly 10 digits:


The number you want is in the first capture group.
(Groups that start with "(?:" are non-capture groups.)

For details see: regex101: build, test, and debug regex

EDIT:  2017-09-28  2:59 PM CT

When I tested this with your macro, it failed.
To work, you need to add a _Search Variable_ Action as the first Action in the _For Each_ block:

<img src="/uploads/default/original/2X/c/c7edec65544f078669d4d9a5cd44adabf7eee36f.png" width="424" height="607">

### Example Results

<img src="/uploads/default/original/2X/5/56a1e1de39d8af3b3948b9f1e1d9b0b678957f6c.png" width="351" height="205">
1 Like

The above solution by @gglick is a good start.
Here's my revision to his macro to exclude capturing 10 digits of a number > 10 digits.

##Macro Library   Isolate 10-Digit Numbers @gglick (Rev 1 by @JMichaelTX)

<a class="attachment" href="/uploads/default/original/2X/0/0864373d1b5e1f4440b387581b1668ef56f2e0bd.kmmacros">Isolate 10-Digit Numbers @gglick (Rev 1 by @JMichaelTX).kmmacros</a> (7.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


<img src="/uploads/default/original/2X/e/e4f052395ee674631f58255358def624ec6f794a.png" width="550" height="1248">

Next step:

This number automatically past on the web page One number one page

How you navigate between pages is not clear. So here's a general set of steps you can use and modify to fit your needs:

At the end of the current macro, you have the list of numbers in the KM Variable "Local__TestResult". I would probably rename this to something like "Local__NumberList". I'll use that going forward.

Add these steps/actions:

  1. IF %ChromeURL does not contain "", then
  • Open URL ""
  • Pause N seconds until page is loaded. Might be 1 or 2 sec.
  • Use a For Each action (KM Wiki) on the lines collection of Variable Local__NumberList.
    • The "For Each" variable should be "Local__Number"
    • Paste action with %Variable%Local__Number"
    • Type Keystoke RETURN
    • Pause N seconds
    • What you do next depends on how to navigate to the next page where you want to paste the next number.

Try you hand at building the macro from here. It may take some trial and error, but that's how you learn.

If you have not done so, go through the Getting Started process.

Getting Started with Keyboard Maestro

  • Read the Quick Start.
    • This is essential to become familiar with KM terminology
  • Do the tutorial (Help ➤ Tutorial) in the KM Editor.
    • Gives you a live walkthrough of creating a macro in the KM Editor
  • Review/Browse the Available Macro Actions
  • For Help with an Action in your Macro, click on the gear icon at the top right of the Action, and select "Help"
  • Search the Keyboard Maestro Wiki for detailed help.
  • Start small, and grow your macros organically.
  • Be prepared for some trial and error in the beginning.
  • Make good use of this Keyboard Maestro Forum
    • Search for existing macros
    • Post your questions/issues if you get stuck

Thanks for improving my macro! I didn't build the original to accommodate for numbers with more than 10 digits just because it seemed like for the OP's needs, there would only ever be 10 digit numbers to capture, but of course you're right in that it's better to be precise (especially after I went and named the macro "Isolate 10 Digits" :sweat_smile:).

I would like to clarify something for anyone else reading this who may be less familiar with regex, though. When @JMichaelTX said

it initially read to me as if my original macro would capture numbers of more than 10 digits, which confused me for a moment since the {MIN,MAX} pattern would have ensured even in the original that exactly 10 digits would be captured. In order to avoid this confusion for anyone else, let me explain: while the \d{10} pattern I used in my original version will always capture exactly 10 digits, and so will not technically allow numbers with more than 10, the problem that @JMichaelTX fixed is that it would also capture the first 10 digits of any number that was 11 digits or more. In other words, instead of ignoring 11 digit numbers like 85639573057 or 12 digit ones like 308972345543, it would capture the first 10 digits from each, resulting in 8563957305 and 3089723455, respectively. @JMichaelTX's pattern in the improved macro fixes this by ensuring that each sequence of 10 digits is bookended by either the start or end of a line or a non-digit, which causes it to ignore numbers of 11 digits or more.

Good point. Sorry for the confusion. I have updated both of my posts to hopefully make it clear:

1 Like

A post was split to a new topic: How Do I Extract Greek Text from My Source Text?