in reply to match whitespace or beginning/end of string

Some solutions:

However, I don't see why the following doesn't suffice:

/\b(beta="second")/

You're obviously not writing a validator, and the double quote mark is unambiguously the end of what you want to match. The \b at the start is necessary to avoid accidentally matching alphabeta="fourth", but there's no need for anything similar at the end since the double quote can't be part of anything else.

Update: I just noticed

a general-purpose rule which works even without the quotes.

Again, no problem

/\b$id=(?:"[^"]*"|\w+)/

Replies are listed 'Best First'.
Re^2: match whitespace or beginning/end of string
by azadian (Sexton) on Nov 02, 2009 at 15:02 UTC
    \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.

      \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"