tags:

views:

120

answers:

5

Hi,

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

  • 1 Week 5 Days 2 Hours 1 Minutes where
  • or 3 minutes
  • Or 5 Days 1 Minute
  • Or 2 Hours etc.

I want to extract the values using a regular expression.

How can I achieve this in .Net?

+4  A: 

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.

UberAlex
+1 That’s what I just wanted to post.
Gumbo
+1 from a hardcore regex user, this isn't a good use for regex, since it would be multiple pass regex. It will be hard to maintain if you try and become tollerant of alternate spellings and other logic. If you are really interested there is a library written in ruby called chronos, which does this kind of input specifically, its source might be valuable to see how they did it.
DevelopingChris
A: 

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?)
samjudson
A: 

Hi there.

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

Cheers. Jas.

Jason Evans
+1  A: 

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);
Stevo3000
A: 

Thanx everyone for such quick response.

ZafarYousafi
-1: Please thank people in comments and/or (preferably) by +1ing. The answers section is for items fitting that descrtiption
Ruben Bartelink