Regex Lesson needed

Hey Folks,

I rarely ask for help and performed a few hours of due diligence and searches. Don't embarrass me too much please.

I've been trying to learn Regex, using the www.regex101.com site, and trying to incorporate the expressions for use in KM. I'm failing.

Here is my Full issue:

First, I'm trying to make this -
CHGO1249987 Add Windshield to Car
into this CHGO1249987 - Add Windshield to Car

I use the following search and replace, in Keyboard Maestro

but since KM globalizes the \s (space), I get CHGO1249987 - Add - Windshield - to - Car. Unlike how it does on the regex101 site as I can turn the global flag off. I think I understand that KM's regexs are global. Is there a flag to stop this or force it to the first instance?

The entirety, of what I'm trying to solve, is to change the first O (in CHGO1249987) to 0 and the first space after, CHGO1249987 , to a "-" (dash).

The final would simply be: CHG01249987 - Add Windshield to Car

Asking for me and a friend with my exact same name.

Thanks much!

KC

Is what you are trying to process always of this form:

<job code><space><description>

If it is, does the <job code> always end with a digit like the ‘7’ in your example?

If it does then my regex would be:

  1. Search for (\d\s)
  2. Replace it with $1-

(There’s a space after that last hyphen.)

I’m not at my Mac so I can’t show the actual search and replace action.

PS: best wishes to you and your friend :+1:

2 Likes

@tiffle,

This worked lovely. I thank you and my friend thanks you!

Thank you for the lesson. :slight_smile:

KC

No problem, but I am going to embarrass you. :wink:
I think you know that you should always post BOTH the source string and the resultant string using a forum Code Block, so that all non-printing characters are properly retained.

Here's my suggestion:

SEARCH FOR:
(.+?)\h+(.+)

REPLACE WITH:
\1 - \2

See: (.+?)\h+(.+)

This is very easy.

SEARCH FOR:
(CHG)O

REPLACE WITH:
\10

I put "CHGO" in parenthesis to make it a Capture Group, so that I could just reference it in the Replace, rather than retype it, and maybe make a typo. :wink:

Glad to see you're learning RegEx. It is very powerful, and once you get over the initial hump, you will find uses for it every day. BBEdit is a great tool to massage text.