in reply to why is regex not matching final character?

In the case of the first regex, the [^\s] matches the last character of the name. But since it's not inside the round parenthesis, it is not included in $1.

Replies are listed 'Best First'.
Re^2: why is regex not matching final character?
by fiverivers (Novice) on May 07, 2012 at 08:19 UTC

    That must be correct. I thought \w would gobble up the whole table name first and [^\s] would stop the gobbling at the first space. But it is matching as much as possible and excluding the last non-space character because it is not in the brackets as you said.

      [...] always matches exactly one character, so the previous \w* has to backtrack and give up one character, in order for the whole match to succeed.

      By the way you can write [^\s] simpler as \S.

      The problem here is \w and \S can match the same characters, so if you match a sequence of \w and \S, the rules about which matches what are governed by the backtracking rules of the regex engine, not by what your intuition expects.

      If you want to match a word, and then want to allow non-word but also non-whitespace characters, you can say (\w+)\s*. The \s* allows empty non-whitespace character sequences too. The regex engine greedily matches as many characters as possible with the \w+, and happily leaves \s* to match the empty string if the following character is a space.

        sounds like you know your regexes. Thinking about the whole thing again all I need is the code below because \w does not match spaces so will stop at the first space.

        if ($line =~ m/^update\ (\w*)/){ print $1."\n"; }