-1

I am trying to fetch just the text within the double angle brackets: from a text like this , However it returns me the whole string, while i just want to fetch the text within the double <<>> brackets.

line2="Name: <<NAME>>   (<<COURSE>>)"
var pattern=@"<<.*?>>";
MatchCollection matches= Regex.Matches(line2,pattern);

Am i doing it correctly , please guide?

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
ShrawaniSilwal
  • 117
  • 3
  • 12

1 Answers1

0

Use this grouping construct: (?< name > subexpression ), like:

string line2 = "Name: <<NAME>>   (<<COURSE>>)";
var pattern = @"<<(?<value>\w+)\>>";
MatchCollection matches = Regex.Matches(line2, pattern);
string name = matches[0].Groups["value"].Value; // "NAME"
string course = matches[1].Groups["value"].Value; // "COURSE"

It puts the captured subexpression into a named group (here "value"). To access the matches use the Groups array, just like I did it above.

Further information provides the quick-reference for regular expressions.

oRole
  • 1,316
  • 1
  • 8
  • 24