[Solved] Regex: matching up to the first occurrence of a character

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.

Solution #1:

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.

Respondent: Leon Fedotov

Solution #2:




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

Respondent: sleske

Solution #3:


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.

Respondent: RJFalconer

Solution #4:

Try /[^;]*/

Google regex character classes for details.

Respondent: Glenn Slaven

Solution #5:

Try /[^;]*/

That’s a negating character class.

Respondent: Dan Breslau

Solution #6:

sample text:

"this is a test sentence; to prove this regex; that is g;iven below"

If for example we have the sample text above, the regex /(.*?;)/ will give you everything until the first occurence of semicolon (;), including the semicolon: "this is a test sentence;"

Respondent: Skilldrick

Solution #7:

This was very helpful for me as I was trying to figure out how to match all the characters in an xml tag including attributes. I was running into the “matches everything to the end” problem with:


but was able to resolve the issue with:


after reading this post. Thanks all.

Respondent: poncius

Solution #8:

None of the proposed answers did work for me. (e.g. in notepad++)



Respondent: Yardboy

Solution #9:

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];


$ php test.php
match everything until first
Respondent: Lonzak

Solution #10:

This will match up to the first occurrence only in each string and will ignore subsequent occurrences.

Respondent: ghostdog74

Solution #11:

"/^([^/]*)/$/" worked for me, to get only top “folders” from an array like:

a/   <- this
c/   <- this
f/   <- this
Respondent: mchid

Solution #12:

Really kinda sad that no one has given you the correct answer….

In regex, ? makes it non greedy. By default regex will match as much as it can (greedy)

Simply add a ? and it will be non-greedy and match as little as possible!

Good luck, hope that helps.

Respondent: sPooKee

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy