tags:

views:

776

answers:

6

Hey, 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 the semicolon.

+3  A: 

Try /[^;]*/

Google regex character classes for details.

Dan Breslau
Beaten to the buzzer!
Skilldrick
Yes, the speed of answers here never ceases to amaze me.
sleske
Luck of the draw -- I think this is the first time it's landed my way ;-)
Dan Breslau
+9  A: 

/^[^;]*/

The [^;] says match anything except a semicolon. The square brackets are a set matching operator, it's 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.

Glenn Slaven
Be aware that the first ^ in this answer gives the regex a completely different meaning: It makes the regular expression look only for matches starting from the beginning of the string. In this case, that would effectively be a no-op *if* you run the regular expression only once. If you want to look for multiple matches within a single string, the first ^ would have to go.
Dan Breslau
He did say that he wanted to match everything until the first occurrence of a semicolon, so I assumed that he meant from the start of the string.
Glenn Slaven
+4  A: 

Try /[^;]*/

That's a negating character class.

Skilldrick
+4  A: 

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.

sleske
+8  A: 

Would;

/^(.*?);/

work?

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

RJFalconer
(I suck at regex, so this is for my understanding as much as anything else)
RJFalconer
Yes, this would work.
slebetman
Yep, remember TMTOWTDI is a perl moto :) http://en.wikipedia.org/wiki/TMTOWTDI
Glenn Slaven
ya, but following the bicarbonate extension to Tim Toady, I believe negated character classes win as lazy quantifier includes backtraking. +1 anyway.
Amarghosh
Worth reading on the performance topic: http://blog.stevenlevithan.com/archives/greedy-lazy-performance
Glenn Slaven
+1  A: 

this is not a regex solution, but something simple enough for your problem description. Just split your string and get the first item from your array.

$str = "match everything until first ; blah ; blah end ";
$s = explode(";",$str,2);
print $s[0];

output

$ php test.php
match everything until first
ghostdog74