Regex: matching up to the first occurrence of a character

Regex: matching up to the first occurrence of a character

You need

/[^;]*/

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

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.

Would;

/^(.*?);/

work?

The ? is a lazy operator, so the regex grabs as little as possible before matching the ;.

Regex: matching up to the first occurrence of a character

/^[^;]*/

The [^;] says match anything except a semicolon. The square brackets are a set matching operator, its essentially, match any character in this set of characters, the ^ at the start makes it an inverse match, so match anything not in this set.

Leave a Reply

Your email address will not be published.