简体   繁体   中英

C# RegEx, first match

I have next string:

String a = "someThing({Some text}); OtherStuff({Other text});";

I need to get the text included in '{' and '}' inside the 'someThing({...})';

I wrote:

var data = Regex.Match(a, @"(?<=someThing\(\{).*?(?=\}\)\;)", RegexOptions.Singleline).Groups[0].Value;

But as a result in data I get the whole string 'a'. My expected result - "{Some text}"

Thanks for any advance.

The below regex would match zero or more characters but not of } , or } which was present just after to something({

(?<=someThing\({)[^{}]*

DEMO

If you want the output to contain {} braces then you need to get out the opening curly bracket from lookbehind.

(?<=someThing\(){[^{}]*}

DEMO

IDEONE

(?<=someThing\()({[^{}}]*})

Try this.See demo.

http://regex101.com/r/jT3pG3/31

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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