David
David

Reputation: 16067

Regex non capturing group ignored?

I have a string like this:

  <Field ID="{2B35B1DD-822A-47E2-9F8C-77006123FA45}"
       Name="NCPE_QualificationTitle"
       StaticName="NCPE_QualificationTitle"
       DisplayName="$Resources:Exigy.NCPE.ProfessionalDirectory.Structure,Fields_NCPE_QualificationTitle_DisplayName;"
       Description="$Resources:Exigy.NCPE.ProfessionalDirectory.Structure,Fields_NCPE_QualificationTitle_Description;"
       Group="$Resources:Exigy.NCPE.ProfessionalDirectory.Structure,NCPE_Group;"
       Type="Text"
    Required="FALSE" />

I'm trying to use this regex:

(?:\$Resources:.*,).*(?:;)

Which I thought should give me Fields_NCPE_QualificationTitle_DisplayName as it's the only part in a non capturing group. However it's matching this string:

"$Resources:Exigy.NCPE.ProfessionalDirectory.Structure,Fields_NCPE_QualificationTitle_DisplayName;"

What am I doing/understanding wrong?

Upvotes: 0

Views: 139

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174834

You just need to turn the first non-capturing group to positive lookbehind (?<=...)and the second one to positive lookahead (?=...).

(?<=\$Resources:.*,).*(?=;)

IDEONE

Regex rgx = new Regex(@"(?<=\$Resources:.*,).*(?=;)");

By default, it would print the Groups[0] ie, index 0 which contains all the matched characters.

Upvotes: 1

vks
vks

Reputation: 67988

(?:\$Resources:.*,)(.*)(?:;)

Try this and grab the capture or group 1 from it.See demo.

https://regex101.com/r/vN3sH3/68

Your regex does not group any data so whole match is being returned.Once you introduce a group the you can access it by specifying group 1 or group 2

Upvotes: 0

Related Questions