Regex and substituion

Hi

have a series of text as below separated into paragraphs like this:
:star:(1) 4:55,york,Coolfitch
lb,14/1(12/1),£10,win 15

Using regex I have got this far as matching goes

but cannot figure out how to get this into and out of KM so I end up with

4:55,york,Coolfitch,14/1(12/1),£10,win,15

I just get baffled. Walk away. Come back. (Many years ago I could not understand why when writing on a floppy disc it did not register with the computer) - Yes it seems that what is obvious to others can seem quite arcane to me till the lightbulb goes off.

We need more information of your workflow.
(What do you mean ‘into’ and ‘outoff’ keyboard maestro? Be specific please)

This is a good question. There are a number of ways to get data into Keyboard Maestro (KM), and then output the results of your macro. They are all easy, but it may be not obvious.

###Input Data into KM Macro

  1. Set a Variable in the Macro directly
  2. Use data already on the Clipboard
  3. Copy text selected by User to Clipboard
  4. Set a KM Variable to the Clipboard
  5. Read text from a File
  6. Prompt User for Input
  7. Use a Script to get/calculate data, and set to KM Variable

###Output Data from a KM Macro

  1. Display results a popup window
  2. Copy Results to Clipboard
  3. Save Results to a File
  4. Use a Script to further process the results

I'll provide details on each later, but for now, here is a simple Macro to get you started. It does the following:

  1. Allows you to set the source text directly in a KM variable BEFORE you run the Macro
  2. Uses RegEx to extract the parts of the text of interest
  3. Recombines these parts into a new KM Variable
  4. Displays the result

##regex

##example Results

##Macro Library   RegEx How to Use withKM


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/4/49801f9d7eebb578523410817af2ecd77dde9c68.kmmacros">RegEx How to Use withKM.kmmacros</a> (5.6 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

###Use Case

* Extract Parts (substrings) of Data from Multiline Text, and recombine into new string (KM Variable)

---

###ReleaseNotes

Author.@JMichaelTX

**NOTICE: This macro/script is just an _Example_**

* It is provided only for _educational purposes_, and may not be suitable for any specific purpose.
* 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.

**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.
* 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 Actions that are shown in the magenta color

**USE AT YOUR OWN RISK**

* While I have given this limited testing, and to the best of my knowledge it 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.


---

<img src="/uploads/default/original/2X/e/e448c4eac6c75252523195eb98319aff57b3bb49.png" width="459" height="956">
2 Likes

Ah superbly answered. You tackled ‘all’ input and output possibilities! I’m rather new to KM and to see this, so simply put, helps a lot. Thanx👍

Michael

this is amazing. Just this alone will help me enormously. Not just with current query but in getting my head round KM. Am sure this will help a lot of KM users.

Many thanks

Michael

2 Likes

I have got this far

No hair left but learnt some things. Use ( ) to separate groups of matches? Use $ signs for replacing. These 2 things I was getting really stuck on as did not know!

Final thing - how do I get rid of blank lines? My output looks like this and want to get rid of blank lines.

Thanks

Michael

This should work. Place it after your "Search and Replace" Action:

It uses this RegEx:
(?m)^\s*\n

Parentheses () are used to create RegEx Capture Groups.
The $n represents each Capture Group in the order defined in the RegEx pattern.
I think most of this is explained in Regular Expressions (KM Wiki), or in one of the documents it references. This is one good references:

So far - great.
Original text is this:

:star:(5) 6:20,killarney,Ishebayorgrey
wh,10/1(17/2),£10,win

which I have got to this, deleting blank lines
6:20,Killarney,Ishebayorgrey

Now I am trying to figure out how to get 10/1 and (17/2) into decimal + 1.

Have got this far

But cannot figure out how to differentiate the 2 fields, viz. 10/1 and (17/2) and Calculate individually. I get that I will have to set 2 Variables. Just cannot figure out how to filter and assign to Variables using regex.

Just breaking down process and then put together once understand elements of process.

Thanks

Michael

It is best to state your full requirements at the start, and give real-world examples of:

  • Source data
  • Desired result

Use the Search Variable action (KM Wiki) (or the Search Clipboard).

Like I showed you above:

I don't know what this means:
" how to get 10/1 and (17/2) into decimal + 1"

Please provide a detailed example with expected results.

“I’ll take those odds”. :slight_smile:

I think the OP wants “10/1” to become 10 - as a number. Likewise “17/2” to become 8.5 - as a number.

Overview:

Bets come through via Telegram and are copied and pasted into TextEdit:

Source data is of the form:
:star:(17) 6:25,Killarney,Airlie Beach
Pp,7/2(3/1),£20,Win

Ultimate Desired Pasted Result:
6:25,Killarney,Airlie Beach,4.59,4.08,1

I know how to delete ":star:(17) " using regex.
I know how to search for the 2 occurring fractions (\d{1,2}/\d{1,2}) using regex.

I can convert manual selection individually using

But I don't know how to search for first and second occurrences of (\d{1,2}/\d{1,2}) and then apply individual calculations and inserts.

The calculation converts the odds from UK to decimal, i.e. (x/y)+1 but I have to allow for betting exchange commission therefore multiply by 1.02 (2% commission) to arrive at max,min odds range.

Have figured out how to calculate and insert by manual selection but cannot figure out how to select the first occurrence, then the second and apply the calculations individually.

The final £ can be £10 or £20 0r £30 and for these I just want to substitute 1, 2 or 3 to denote points to be staked rather than using currency values.

Think I can figure this out using if, else statement.

Your example does NOT match this. Source is "£20", which should result in "2" points, correct?

IAC, that is the assumption I have made in this revised macro.
You could have saved both of us much time, if you had posted all of this at the beginning. :smile:


Given a Source of:
> ⭐️(17) 6:25,Killarney,Airlie Beach
> Pp,7/2(3/1),£20,Win 

the macro produces this result:
<img src="/uploads/default/original/2X/c/c09308f94bcac55117b8b3a16c5b0c96be1ef5ee.png" width="322" height="186">

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

###MACRO:&nbsp;&nbsp;&nbsp;RegEx Extract Multiple Fields from Multiple Lines @Example

~~~ VER: 2.0&nbsp;&nbsp;&nbsp;&nbsp;2017-07-14 ~~~

####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/d/d03221d16e47ef53d1b458235bd1775c076b6b57.kmmacros">RegEx Extract Multiple Fields from Multiple Lines @Example.kmmacros</a> (7.4 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

###Regex
`^[^\(]+\(\d+\)[ \t]+(.+)\n(?:lb|Pp),(\d+\/\d+)\((\d+\/\d+)\),£(\d)`

Details can be found here:  https://regex101.com/r/Jwf2Ru/4

Note that since your first post had "lb" rather than "Pp" in the text, I  have allowed for either.

###ReleaseNotes

Author.@JMichaelTX

**NOTICE: This macro/script is just an _Example_**

* It is provided only for _educational purposes_, and may not be suitable for any specific purpose.
* 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.

**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.
* 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 Actions that are shown in the magenta color

**DISABLE OR DELETE THIS ACTION FOR PRODUCTION USE**

<img src="/uploads/default/original/2X/3/34e9e04225ed2e18194e9864a6f607db7a6a9601.png" width="521" height="162">

**USE AT YOUR OWN RISK**

* While I have given this limited testing, and to the best of my knowledge it 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.


---


<img src="/uploads/default/original/2X/c/c890936900a77f6b187565e8e86cbbe99e44dbb1.png" width="556" height="1464">

Oh, BTW, the calculations do not include my commission. :wink: 
Just kidding!  LOL