[regex] 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.

This question is related to regex

The answer is


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.


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

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

^.*?(?=\;)

did.


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

a/   <- this
a/b/
c/   <- this
c/d/
/d/e/
f/   <- this

Would;

/^(.*?);/

work?

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


/^[^;]*/

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.


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

/^([^;]*);*/

I found that

/^[^,]*,/

works well.

',' being the "delimiter" here.


Try /[^;]*/

That's a negating character class.


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:

/<simpleChoice.*>/

but was able to resolve the issue with:

/<simpleChoice[^>]*>/

after reading this post. Thanks all.


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


Try /[^;]*/

Google regex character classes for details.