in reply to Re: match whitespace or beginning/end of string
in thread match whitespace or beginning/end of string

\b and \s don't work if the substring to be matched comes at the beginning of the string. There are, as you pointed out, ways to deal with this, but they are too complicated for my users.
  • Comment on Re^2: match whitespace or beginning/end of string

Replies are listed 'Best First'.
Re^3: match whitespace or beginning/end of string
by ikegami (Patriarch) on Nov 02, 2009 at 15:36 UTC

    \b and \s don't work if the substring to be matched comes at the beginning of the string.

    If you're going to contradict, please test first. You would have found yourself wrong. The beginning and the end of the string count as whitespace for \b. This is documented and observable:

    $_ = 'alpha="first" beta="second" gamma="third"'; for my $id (qw( alpha beta gamma )) { my ($val) = /\b$id=("[^"]*"|\w+)/ or next; print("$id: $val\n"); }
    alpha: "first" beta: "second" gamma: "third"

    Or

    $_ = 'alpha="first" beta="second" gamma="third"'; while (/(\w+)=("[^"]*"|\w+)/g) { print("$1: $2\n"); }
    alpha: "first" beta: "second" gamma: "third"