459,299 Members | 1,225 Online
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

# Extracting numbers from a string

 P: n/a I have a number of strings that represents time. 1w 2d 3h 15m 2d 3h 15m 4h 30m 45m I want to extract the number parts of my strings into separate variables for Weeks, Days, Hours and Minutes. Any body got any good ideas about how to do this? Oct 4 '07 #1
10 Replies

 P: n/a "Dan"

 P: n/a "Dan" I have a number of strings that represents time. 1w 2d 3h 15m 2d 3h 15m 4h 30m 45m I want to extract the number parts of my strings into separate variables for Weeks, Days, Hours and Minutes. Any body got any good ideas about how to do this? Air code: The string.Split method will let you build an array of each line eg myArray = sLine.split(" ") You could use the string.Contains method to identify if it is a week hour etc Perhaps an array of Stuctures to store the rows of data in... Just a couple of ideas..... Oct 4 '07 #3

 P: n/a I'd look into regular expressions. Good Luck, Bob "Dan" I have a number of strings that represents time. 1w 2d 3h 15m 2d 3h 15m 4h 30m 45m I want to extract the number parts of my strings into separate variables for Weeks, Days, Hours and Minutes. Any body got any good ideas about how to do this? Oct 5 '07 #4

 P: n/a Dan, Like Harry I would just simple use the split for this. Cor Oct 5 '07 #5

 P: n/a I was hoping i could do it in one line with a regular expression rather than creating a function that splits it and searches for specific things. Anybody got any example regular expressions i could use? Oct 5 '07 #6

 P: n/a On Oct 5, 6:33 am, Dan \d{1,}(?=w))").Value) Dim day As Integer = CInt(Regex.Match(dateString, "(? \d{1,}(?=d))").Value) Dim hour As Integer = CInt(Regex.Match(dateString, "(? \d{1,}(?=h))").Value) Dim minute As Integer = CInt(Regex.Match(dateString, "(? \d{1,}(?=m))").Value) Console.WriteLine("The Week is {0}", week.ToString()) Console.WriteLine("The Day is {0}", day.ToString()) Console.WriteLine("The Hour is {0}", hour.ToString()) Console.WriteLine("The Minute is {0}", minute.ToString()) Console.Read() End Sub End Module ///////////////////////// Thanks, Seth Rowe Oct 5 '07 #7

 P: n/a That worked a treat, thanks very much. And thanks for pointing me in the direction of Expresso too, should prove very helpful in the future. Oct 5 '07 #8

 P: n/a Dim myTimetable As Hashtable Dim myHashtable As New Hashtable myHashtable.Add("Weeks", "w") myHashtable.Add("Days", "d") myHashtable.Add("Hours", "h") myHashtable.Add("Minutes", "m") myTimetable = ParseTimeString(myHashtable, "1w 2d 3h 15m") Debug.Print("Weeks=" & myTimetable("Weeks").ToString _ & " Days=" & myTimetable("Days").ToString _ & " Hours=" & myTimetable("Hours").ToString _ & " Minutes=" & myTimetable("Minutes").ToString) Function ParseTimeString(ByVal inHashtable As Hashtable, ByVal inString As String) As Hashtable Dim myRegex As System.Text.RegularExpressions.Regex Dim myMatch As System.Text.RegularExpressions.Match Dim myDictionaryEntry As DictionaryEntry Dim Timetable As New Hashtable For Each myDictionaryEntry In inHashtable Dim myPattern As String myPattern = "(?<" & myDictionaryEntry.Key & ">\d{1,}(?=" & myDictionaryEntry.Value & "))" myRegex = New System.Text.RegularExpressions.Regex(myPattern) myMatch = myRegex.Match(inString) If myMatch.Success = True Then Timetable.Add(myDictionaryEntry.Key, myMatch.Groups(myDictionaryEntry.Key).Value) Else Timetable.Add(myDictionaryEntry.Key, vbNull) End If Next Return Timetable End Function Oct 5 '07 #9

 P: n/a Dan, I was hoping i could do it in one line with a regular expression rather than creating a function that splits it and searches for specific things. Why you want a slow solution instead of a quick one? Cor Oct 6 '07 #10

 P: n/a On Oct 6, 6:32 am, "Cor Ligthert[MVP]" wrote: Dan, I was hoping i could do it in one line with a regular expression rather than creating a function that splits it and searches for specific things. Why you want a slow solution instead of a quick one? Cor Are you saying that regular expressions are slower than looping through an array of strings? Oct 11 '07 #11

### This discussion thread is closed

Replies have been disabled for this discussion.