Anton Kozlovsky
Anton Kozlovsky

Reputation: 213

regex expression last match C#

I have string like this:

   test- qweqw (Barcelona - Bayer) - testestsetset

And i need to capture Bayer word.I tried this regex expression ( between "-" and ")" )

(?<=-)(.*)(?=\))

Example: https://regex101.com/r/wI9zD0/2 As you see it worked a bit incorrect.What should i fix?

Upvotes: 1

Views: 54

Answers (2)

w.b
w.b

Reputation: 11238

You don't need regex for that, you can use LINQ:

string input = "test - qweqw(Barcelona - Bayer) - testestsetset";

string res = String.Join("", input.SkipWhile(c => c != '(')
                                  .SkipWhile(c => c != '-').Skip(1)
                                  .TakeWhile(c => c != ')'))
                   .Trim();

Console.WriteLine(res);  // Bayer

Upvotes: 0

Travis Schettler
Travis Schettler

Reputation: 854

Here's a different regex to do what you are looking for:

-\s([^()]+)\)

https://regex101.com/r/wI9zD0/3

Upvotes: 1

Related Questions