Regex code that works on regex101.com but not in KM macro

Example text at end of post.
The goal is to match 4 elements:

Street address
City
State (whether typed out full or 2 digit abbreviation)
Zip

thank you to @ccstone
I can get all but the state with:
Lead status\s+(\w+.)\v+(\w+[^,]+).(\d{5})

The following works to get all 4 matches on regex101.com
but does not work in the KM macro:
Lead status\s*(\w.)\R([^,]+),\s([^,][^\s,])\s.*(\d{5})$

Lead status

11123 W 70th St
New York, New York 10023

Hey @troy,

Your pattern doesn’t work for me on regex101.

You can save stuff on regex101 and share the URL, so people can see directly what you were working on. That saves everyone time, effort, and increases accuracy.

Here's a test-case:

RegEx ⇢ String ⇢ KM Find and Replace RegEx ⇢ Test 01.kmmacros (5.0 KB)

Keep in mind that regex101's primary syntax is PCRE (Perl Compatible Regular Expressions).

Keyboard Maestro uses ICU Regex.

You can find the above page in the Keyboard Maestro Editor's Help menu along with the ICU Date Format Reference.

Generally if it works with PCRE, it'll work with KM – but there are some differences.

Unfortunately I can't list them out off the top of my head.

Lead status\s*(\w.+)\v(\w[^,]+),\h([a-z ]+)\h+(\d{5})

Lead status	== 	Literal text
\s*			== 	any kind of space 0 or more
(\w.+)		== 	capture 1 word-character then any character 1 or more.
\v			== 	vertical whitespace
(\w[^,]+)	== 	capture 1 word character then 1 or more of any character NOT “,”
,			== 	Literal text
\h			== 	horizontal whitespace
([a-z ]+)	== 	capture class a thru z and space - 1 or more
\h+			== 	horizontal whitespace 1 or more
(\d{5})		== 	capture digits - exactly 5

I should have enforced 1 or more on that first \s, but 0 or more does work.

I say this, because when reading the regular expression it is more clear what it expects for input.

-Chris

1 Like

Thank you for the new macro (it works great) and the explanations, I will use them. It's been a learning experience. Cheers

Here's what I used up on Regex101

Test-Case-Page

Hey @troy,

This is just a little different than what you posted above:

Lead status\s*(\w.*)\R([^,]+),\s*([^,]*[^\s,])\s*.*(\d{5})$

So you can clearly see why using the saved pattern feature is so helpful.

Your pattern (above) works for me in my test-case macro. (Like the one I posted.)

Does it not work for you?

-Chris

Correct
Your post of
Lead status\s*(\w.+)\v(\w[^,]+),\h([a-z ]+)\h+(\d{5})
Does work in a KM macro for me

Lead status\s*(\w.)\R([^,]+),\s([^,][^\s,])\s.*(\d{5})$
Does not work in a KM macro for me

I'm fine using your macro (code) which works perfectly for my needs.

Hey @troy,

Note that the pattern you just posed is different than the one in the regex101 test-case you posted earlier.

Keeping track of all the things you try can be confusing!  :wink:

-Chris

1 Like