Regex: matching up to the first occurrence of a character

You need

/^[^;]*/

The [^;] is a character class, it matches everything but a semicolon.

^ (start of line anchor) is added to the beginning of the regex so only the first match on each line is captured. This may or may not be required, depending on whether possible subsequent matches are desired.

To cite the perlre manpage:

You can specify a character class, by enclosing a list of characters in [] , which will match any character from the list. If the first character after the “[” is “^”, the class matches any character not in the list.

This should work in most regex dialects.

Leave a Comment