MACRO: Extract Data with RegEx from End of URL @Example
VER: 1.0 2017-05-18 ~~~
DOWNLOAD:
Extract Data with RegEx from End of URL @Example.kmmacros (7.8 KB)
Use Case
This macro uses a very specific RegEx in response to the requirements in this topic:
Extracting Text from URL
Most likely anyone else who wants to use this macro will need to change the RegEx to fit their needs.
However, this macro does illustrate an approach that can be used in general.
Example Results
ReleaseNotes
Author.@JMichaelTX
PURPOSE:
- Demo How to Extract Specific Data from URL
NOTICE: This macro/script is just an Example
- It has had very limited testing.
- You need to test further before using in a production environment.
- It does not have extensive error checking/handling.
- It may not be complete. It is provided as an example to show you one approach to solving a problem.
HOW TO USE:
- Change the "Set Variable EDU_SourceText" to be the URL you want to use
- Review the RegEx Solutions to choose the one best for your use case
- 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.
.
- Assign a Trigger to this maro. I prefer TBD.
- Move this macro to a Macro Group that is only Active when you need this Macro.
- ENABLE this Macro.
. -
REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
(all shown in the magenta color)- "Set Variable EDU_SourceText" to be the URL you want to use
- Review the RegEx Options to determine which is best for your use case
TAGS: @RegEx @URL @Extract
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
REQUIRES:
- Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
- El Capitan 10.11.6+
- It make work with Yosemite, but I make no guarantees.
USE AT YOUR OWN RISK
- While I have given this limited 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.
RegEx Solutions
USER CHOICE: Review these solutions and choose the one best for your use case.
Copy the RegEx expresstion and paste into the "Search Variable" Action.
FIRST SOLUTION -- See https://regex101.com/r/tyAOOA/1
Allow for any digit to precede the Capture Groups
This is the most flexible solution.
\/@.+?!\dd([\d.]+)!\dd\-([\d.]+)
SECOND SOLUTION -- See https://regex101.com/r/X37oty/1
Requires specific numbers prior to the Capture Groups:
!3d AND !4d
This is the more reliable solution, assuming these specific numbers will always be in the URL.
\/@.+?!3d([\d.]+)!4d\-([\d.]+)