Reputation: 59
i am having this regex to match on this given data. it must match every reference name(include) and also it should match other values if its given there. source data ..
<Reference Include="Interop.ERMSPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b5fdd3f42e76a9c0, processorArchitecture=MSIL"> <HintPath>C:\Program Files (x86)\Epiplex500\epiplex\Bin\SharedInterop.ERMSPlugin.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="LicenseProcessing">
<HintPath>C:\Program Files (x86)\Epiplex500\epiplex\Bin\Shared\LicenseProcessing.dll</HintPath>
</Reference>
<Reference Include="Snapshot">
<HintPath>C:\Program Files (x86)\Epiplex500\epiplex\Bin\Shared\Snapshot.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
i am using this regex.
<Reference Include="(?:([^,|"]+?)[,|"]|[\s\S]*?Version=([^,]+?),[\s\S]*?PublicKeyToken=([^,|"]+)\S([^">]+?)[\s\S]*?<HintPath>([\s\S]*?)<\/HintPath>[\s\S]*?)
i need version, public key, and path along with given name. its matching only reference name others are ignored. anyone plz help me out here.
thanks
Upvotes: 1
Views: 314
Reputation: 627607
Here is a solution based on the Microsoft.Build
and still using regular expressions:
Add the reference by right-clicking the [Solution] > Add References..., selecting Assemblies > Framework > Microsoft.Build
and then hitting OK.
And here is the code itself (EDIT: now, it should process all Reference nodes):
var project = new Microsoft.Build.Evaluation.Project(@"FILE_PATH.csproj");
var references = project.Items.Where(p => p.ItemType == "Reference").Select(p => p);
foreach (var reference in references)
{
var ReferenceName = Regex.Match(reference.EvaluatedInclude, @"^[\w\.]+(?=,\p{Zs}|$)", RegexOptions.CultureInvariant).Value;
var version = Regex.Match(reference.EvaluatedInclude, @"(?s:(?<=Version=)([\d\.]+))", RegexOptions.CultureInvariant).Value;
var keyToken = Regex.Match(reference.EvaluatedInclude, @"(?s:(?<=PublicKeyToken=)([\w\-]+))", RegexOptions.CultureInvariant).Value;
var hintpath = reference.GetMetadata("HintPath") != null ? reference.GetMetadata("HintPath").EvaluatedValue : string.Empty;
}
EDIT2: In case there are problems with matching the values in the Inlcude attribute, you can also use these regexes that are just matching all non-space characters before a "comma+space" or a line end:
var ReferenceName = Regex.Match(reference.EvaluatedInclude, @"^[^\p{Zs}]+(?=,\p{Zs}|$)", RegexOptions.CultureInvariant).Value;
var version = Regex.Match(reference.EvaluatedInclude, @"(?s:(?<=Version=)([^\p{Zs}]+(?=,\p{Zs}|$)))", RegexOptions.CultureInvariant).Value;
var keyToken = Regex.Match(reference.EvaluatedInclude, @"(?s:(?<=PublicKeyToken=)([^\p{Zs}]+(?=,\p{Zs}|$)))", RegexOptions.CultureInvariant).Value;
Upvotes: 2