ZafarYousafi
ZafarYousafi

Reputation: 10900

Named Groups in Regular Expression

I want to parse the input string and extract the values from it. My input string might have Week, Days, Hours or Minutes.

So, the input string might be

I want to extract the values using a regular expression.

How can I achieve this in .Net?

Upvotes: 1

Views: 372

Answers (4)

Jason Evans
Jason Evans

Reputation: 29186

Here's a rough example of how to parse that text for the various values.

Dim inputString As String = "1 Week 5 Days 2 Hours 1 Minutes"
Dim pattern As String = "(?<Week>\d+)\s*week\s*(?<Days>\d+)\s*days\s*(?<Hours>\d+)\s*hours"

Dim m As Match = Regex.Match(inputString, pattern, RegexOptions.Compiled Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)

If m.Success Then
  Dim hours As String = m.Groups("Hours")
   etc...
End If

Upvotes: 0

stevehipwell
stevehipwell

Reputation: 57568

The following regex matches singular or plural (e.g. days or day) as long as the items come in order.

//Set the input and pattern
string sInput = "1 Weeks 5 Days 2 Hours 1 Minutes";
string sPattern = "^\s*(?:(?<weeks>\d+)\s*(?:weeks|week))?\s*(?:(?<days>\d+)\s*(?:days|day))?\s*(?:(?<hours>\d+)\s*(?:hours|hour))?\s*(?:(?<minutes>\d+)\s*(?:minutes|minute))?";

//Run the match
Match oMatch = Regex.Match(sInput, sPattern, RegexOptions.IgnoreCase);

//Get the values
int iWeeks = int.Parse(oMatch.Groups["weeks"].Value);
int iDays = int.Parse(oMatch.Groups["days"].Value);
int iHours = int.Parse(oMatch.Groups["hours"].Value);
int iMinutes = int.Parse(oMatch.Groups["minutes"].Value);

Upvotes: 2

samjudson
samjudson

Reputation: 56893

Capture groups in Regex are enclosed in brackets (e.g. "(\d+ Week)").

Named capture groups are done using a question mark and the name, "(?<week>\d+ Week)".

They are then returned as follows, m.Groups("week").Value.

The full regex (untested) might look something like this:

  (?<weeks>\d+ weeks?)\s*(?<days>\d+ days?)\s*(?<hours>\d+ hours?)\s*(?<minutes>\d+ minutes?)

Upvotes: 0

UberAlex
UberAlex

Reputation: 3557

I think using a regular expression would be a bit of overkill for this. If I were you, I would just tokenize the string, convert it to lowercase and then switch between the different words. It's a much better way to handle a situation where you have fixed known substrings.

Upvotes: 4

Related Questions