简体   繁体   English

正则表达式最后匹配C#

[英]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 ")" ) 而且我需要捕获Bayer word。我尝试了此正则表达式(在“-”和“)”之间)

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

Example: https://regex101.com/r/wI9zD0/2 As you see it worked a bit incorrect.What should i fix? 示例: https : //regex101.com/r/wI9zD0/2如您所见,它的工作方式有点不正确。我该怎么解决?

Here's a different regex to do what you are looking for: 这是一个不同的正则表达式,可以满足您的需求:

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

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

You don't need regex for that, you can use LINQ: 您不需要正则表达式,可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM