Vlad
Vlad

Reputation: 39

regex matches uppercase letters, spaces, & symbol and apostrophes from begining of string to first comma

I'm trying this regex, but along with uppercase it matches lowercase somehow.

preg_match("/^([A-Z&\s\']+),/i", $line, $match);

Any ideas where is my mistake? Thanks!

Upvotes: 1

Views: 668

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

You need to remove the case insensitive modifier /i:

preg_match('/^([A-Z&\s\']+),/', $line, $match);
                             ^

Otherwise, the [A-Z] range matches both [a-z] and [A-Z] ranges.

Pattern details:

  • ^ - start of string
  • ([A-Z&\s\']+) - Group 1 capturing 1 or more uppercase ASCII letters (A-Z), a literal &, whitespace (\s) or a literal '
  • , - a comma

Upvotes: 1

Related Questions