Calling all regexp experts!

Dan Nigrin's icon

OK, you probably don't even need to be an expert - just better than me!

I want to detect if an expression includes "Keyboard 2" (without the quotes) - the K can be capitalized or not. "Keyboard 2" may appear at the beginning, middle or end of my expression.

How to??

brendan mccloskey's icon

rename your post as follows:
"calling all Luke Halls"

:)

Dan Nigrin's icon

Yeah, where are you Luke!

So I've tried [regexp "K|keyboard 2"] , and that gets me a "keyboard 2" out the substring outlet when I give it an expression that contains "keyboard 2" - so far so good. But then it inappropriately matches a "K" substring for any expression that includes a K....

big_pause's icon

you want [regexp "[Kk]eyboard 2"]

"K|keyboard 2" - will do a or'ed match of either K and keyboard 2

"[Kk]eyboard 2" - matches a character class (group of characters between []), then the rest of the search (eyboard 2)

Dan Nigrin's icon

That's it, thanks big_pause!!

Luke Hall's icon

Oh man! My Max5 license is still on my old laptop and I can't afford Max6 on this machine yet so I haven't been around much. Sorry if you feel I've been neglecting you Dan! ;)

Dan Nigrin's icon

No worries Luke, just messing with ya!

11OLSEN's icon

i saved every successful use of regexp + i collect these examples from any regexp thread appearing here since i don't know, a year or two. all in a big patch. this is already helping me so much when i'm stucked again with regexp. because in most cases one example comes at least close to what i want to do. this might be a good tool release one day after they have been categorised a little bit.
O.

big_pause's icon

There are many online (general) regex test pages out there, a good one is

This has many examples to go through.

Whilst not specific to max, its handy non the less.

Also, you could dive into the Perl regex tutorial, which would help quite a bit

There'll be more in there than is possible with the max regex object, but it's all good stuff

11OLSEN's icon

yes gskinner is worth a bookmark.

Dan Nigrin's icon

Wow, gskinner is very nice, thanks for that tip big_pause!

Dan Nigrin's icon

OK - follow up to my original question in this thread: now I want to detect when there is a "Keyboard" and a "2" in a symbol. But unlike my previous question, the 2 may not necessarily appear right after the Keyboard; it might appear before, or several words after the Keyboard. How to do with regexp?

big_pause's icon

Try this for size.

[regexp "[Kk]eyboard.*2|2.*[Kk]eyboard"]

Dan Nigrin's icon

Thank you again! So that *almost* works - it doesn't however match something like "Trackpad/Keyboard 2", which it should. So I changed your suggestion to [regexp .*[Kk]eyboard.*2|2.*[Kk]eyboard] and it seems to work...

The thing I wasn't familiar with is the "." - does that mean any character or space? I know that the following "*" means "zero or more occurrences of the preceding character."

Luke Hall's icon

The .dot. means anything but a line break, which you shouldn't have to worry about in this case so your example should work just fine.

Dan Nigrin's icon

Cool, thanks Luke.

big_pause's icon

you shouldn't need that .* that you've added

Max Patch
Copy patch and select New From Clipboard in Max.

see this for eg