Search & replace variables

I apologise for being a bit dense but I am trying to do something very simple here.

Take text:
Sandown 1:25 Harley Rebel
Wolverhampton 1:40 Summerinthecity
Ayr 1:45 Billy Billy
Ayr 2:20 Ubaltique
Chepstow 2:25 The Gipper
Ayr 2:55 Maybe I Won’t
Chepstow 3:35 Cloudy Beach
Ayr 4:05 Bernardelli
Chepstow 4:10 Midnight Silver
Sandown 4:20 Kylemore Lough
Chepstow 4:45 Pressurize
Sandown 4:55 Morning Reggie
Chepstow 5:20 Extreme Impact

Copy to clipboard

Search using \w+\s+\d+:\d+\s
Replace first 2 spaces \s with comma ","
Paste clipboard

I just don’t get what KM IS doing or why.

In English, copy the text, search for first 2 occurrences of spaces, replace these found occurrences with comma’s, paste back.

Can someone explain the logic of how this is done?

Many thanks Michael

PS I don’t just want to copy a macro - I want to understand what is going on so I actually learn something.

Post the macro you’ve made so far and then we can explain where you are going wrong.

I cannot figure out how to replace just the space \s only - at least that is what I think I am having trouble with.

Thanks

Michael

  • You were using String Matching
  • You didn’t capture any group
  • No need for a variable, you can directly search/replace the clipboard

This should work:

Or, for the case that the first field (β€œWolverhampton”) might contain a space (β€œWolverhampton South”), this will be safer:
(.+)\s+(\d+:\d+)\s
or
(?m)^(.+)\s+(\d+:\d+)\s

1 Like

Thank you Tom.

Used this and worked fine. (.+)\s+(\d+:\d+)\s

Can I ask? Anything within ( ) is to be ignored and the $1, etc denotes instances where a replacement should be made?

Many thanks

Michael

The (.+) and (\d+:\d+) are captured matching groups. They will be remembered and reinserted into the result with $1 and $2 etc. Anything that is not captured (the two spaces here) will be replaced with the replacement string (commas here).

KM Wiki
Reference
Tester
More info

1 Like

Thanks Tom.

Did not know about the ( ) and $'s - it is what I don’t know I don’t know that stops me in my tracks.

Cheers

Michael

That's true for all of us, and it is hard to get started with RegEx. The syntax is unlike almost anything else I've ever seen. But it is so powerful . . .

As @Tom suggested, see:
KM Wiki Article: Regular Expressions (RegEx)

Lots of great references there. Here are a few I really like:

I use RegEx101.com all the time. It is great for developing and testing new RegEx patterns.

Also finding this useful.

Cheers

Michael

1 Like

Hey Michael,

Not bad, but I started flinching when he claimed \w was the metacharacter for any letter.

\w == any WORD character – meaning any letter, number, or underscore
\w+

Will match:

abc_def_ghi_123_456
\b == word boundary – NOT a space.

A space can help define a word boundary, but so can punctuation...

Make sure you have BBEdit or its freeware sibling TextWrangler to test with.

See also my regular expression cheatsheet for same.

-Chris

3 Likes

Yep, BBEdit still rules :heart_eyes:

Last night this was working fine but not now.

I inserted some pauses just in case but still no joy. I had a sneaky feeling Launchbar might be interfering so turned that off but still no joy.

Running KM 6.4.8 on OS ML.

Any clues on this? Also thanks for all suggestions.

Michael

You have set it to String Matching, again :smile:

What you need is β€˜Regular Expression’:

2 Likes

Hi. I struggled through learning β€” well, getting off the ground, anyway β€” RegEx … it pretty much exemplifies β€œarcane”. I found the following helpful:

RegEx is absolutely great … and a brain-halting deeply-opaque cypher. Hope the above is useful. Good luck!

Thanks

Ordered the book. Also really like https://regex101.com/
Will check out regegg

Thanks again to all who chipped in.

Michael

So do I. It is really great how it shows you the matches immediately as you type in the RegEx pattern. Makes developing a new pattern much easier.

Of course, I also have open the great RegEx Cheat Sheet by @ccstone.

Good find!

-ccs

Did anyone mention this one? http://www.regexr.com/

Michael

Cheatsheet is cool too

A slight problem with this site is that is doesn’t tell me which regex flavor is used. This can lead to some surprises when you paste your β€œtested” regex to your application or script.

As a user pointed out it’s probably the JavaScript regex. Other sites, like regex101 let you chose the flavor (PCRE, JS, Python).

KM uses ICU regexes, which β€œare based on Perl's regular expressions”. So I guess PCRE would be the best choice for testing regexes for KM. But maybe I’m wrong here.