Maris
Maris

Reputation: 4776

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.

Upvotes: 1

Views: 178

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174864

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

Upvotes: 1

vks
vks

Reputation: 67988

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

Try this.See demo.

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

Upvotes: 0

Related Questions