Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: matching up to the first occurrence of a character

Tags:

regex

I am looking for a pattern that matches everything until the first occurrence of a specific character, say a ";" - a semicolon.

I wrote this:

/^(.*);/ 

But it actually matches everything (including the semicolon) until the last occurrence of a semicolon.

like image 949
Leon Fedotov Avatar asked Jan 06 '10 13:01

Leon Fedotov


1 Answers

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.

like image 200
sleske Avatar answered Sep 28 '22 10:09

sleske