Regex working in regex101 but not in KM

This regex
/^(?:M(?:rs?|(?:is)?s).?\s*)?\K\w+(?=.*\nName)/mg
works in demo: regex101: build, test, and debug regex
but not in KM when used as (?mg)^(?:M(?:rs?|(?:is)?s)\.?\s*)?\K\w+(?=.*\nName)
It should match only the first name.
Any idea what is causing this?

Thanks!
-Neo

I'm not experienced enough with RegEx to know why that particular expression is not working... but I just tried this and it works fine with a handful of names and title combos I put in:

^(Mr.|Mr|Ms.|Ms|Mis|Miss|Mrs.|Mrs)\s(.*)\s(.*\nName)

Basically just separating all variants of a person's title with the line separator. Would something like that work for you?

Not quite, the regex should match only the firstname without any pre- or postfixes

A working regex is below...
(I never used the \K token so I don't quite understand it.)

regex fix.kmmacros (1.7 KB)

But I think the main problem lies with the (?g) parameter with is of no use in KM.
see the wiki Regular Expressions [Keyboard Maestro Wiki]
you have to loop through the matches.

Thanks @Joseki, Any chance you can adapt the regex so it only matches the firstname? I know that I can save it from group1, but if possible like to avoid that option.

Sorry for the late reply, I'm working too much and don't have time.
But there's a misconception there : the group1 IS the match of the first name, so I don't see how you can or want to avoid using it.
If you need further help please post your macro to the forum so that we can see what you intend to do.

The problem was the PCRE flavour my original regex was in, which is not/fully supported in KM.