0

My test data

Date:Fri 14-Mar-2003 Venue:S.F.S. Crowd:24,172

The data I am interested in

Fri 14-Mar-2003

The code I currently have

string datePattern = "Date:(.*?) Venue";
string tempDate = Regex.Match(values[(int)HomeColumnNames.VenueCrowdDate], datePattern).Value;

The data that is being returned

Date:Fri 14-Mar-2003 Venue

Any advice or assistance would be much appreciated.

baldie
  • 27
  • 1
  • 5

4 Answers4

1

You are including the words into your regex, so they will be in the extracted string. Try

string datePattern = @"{\w+}\s+{\d{1,2}-{\w+}-{\d{4,}";
bash.d
  • 13,029
  • 3
  • 29
  • 42
1

You can also loop thought the matched groups.

var groups = Regex.Match(values[(int)HomeColumnNames.VenueCrowdDate], datePattern).Groups;
Console.WriteLine(groups[1].Value); //Fri 14-Mar-2003
Vignesh.N
  • 2,618
  • 2
  • 25
  • 33
1

Your regex is ok. Just you need to get group one.

var match= Regex.Match(values[(int)HomeColumnNames.VenueCrowdDate], datePattern);

match.Groups[0]; //returns full match
match.Groups[1]; //returns 1st group

//Gets MatchCollection
var matches= Regex.Matches(values[ManyAddresses, datePattern);

using @bash.d's pattern is better for other samples.

Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
0

You're returning the match, not your capture group.

The following code will allow you to explicitly name (and subsequently reference) your capture. It's not necessary to explicitly name or number your groups... But it's good practice.

String groupName = "yourGroupName";
Regex r = new Regex(@"Date:(?<" + groupName + ">.*?) Venue");
Matches m = r.Match(yourTestData);
Console.WriteLine(m.Groups[groupName]);
Sepster
  • 4,800
  • 20
  • 38