regexp with ^ caret and $ dollar signs

ruviaro's icon

Hi,

I'm having trouble to understand the exact behavior of the caret inside regexp.

regexp .$

This returns the last character of a string ("n" from "horn", "7" from "dx7", etc.). Sounds good to me.

regexp ^.

I would expect this one to return only the first character of a string, such as "h" from "horn" and "d" from "dx7". However, it outputs lists such as "h o r n" and "d x 7".

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

I'm certainly missing something here. Any help appreciated. Please see example patch below. I've searched other regexp-related posts in this forum, but this is not addressed directly.

Luke Hall's icon

The [regexp] object carries on searching regardless. If you want it to match only the first character you need to use the backreferences outlet and use something like regexp ^(.).*. This will match the whole word but only remember the first character, whatever it might be. If you don't do this the object starts searching again from where it left off which leads to the results you pointed out.

lh