Ninja Cowgirl
Ninja Cowgirl

Reputation: 11251

1 liner regex to get everything before semi colon

I have this variable 1874;#Bob Williams. I tried this and it should give me 1874 but it's giving me the entire variable. Any idea?

(?<=\w+;)

Upvotes: 1

Views: 375

Answers (2)

Saeb Amini
Saeb Amini

Reputation: 24419

Whatever tool/engine you're using is removing what it matches and returns what remains after removal, so this should work for you: ;.+

Upvotes: 2

aloisdg
aloisdg

Reputation: 23531

A bit off topic but you dont need a regex for this. Not at all. Use Substringinstead:

var s = "1874;#Bob Williams";
s = s.Substring(0, s.IndexOf(';')); // If your input might not contain a semi colon, check the return of IndexOf
// s == "1874"

Demo

If you are writing .NET code as your tag show, use this.

Upvotes: 1

Related Questions