By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
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
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 4 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Dan" <da**********@newcross-nursing.comschrieb
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?

http://msdn2.microsoft.com/en-us/library/st9zk29t.aspx
Armin

Oct 4 '07 #2

P: n/a
"Dan" <da**********@newcross-nursing.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>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" <da**********@newcross-nursing.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>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
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.

Anybody got any example regular expressions i could use?

Oct 5 '07 #6

P: n/a
On Oct 5, 6:33 am, Dan <dan_willi...@newcross-nursing.comwrote:
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?
I am by no means a Regex expert - I am simply a man with Expresso.

http://www.ultrapico.com/Expresso.htm

But the following may do what you need:

//////////////////////////
Imports System.Text.RegularExpressions

Module Module1

Sub Main()
Dim dateString As String = "1w 2d 3h 15m"

'// These will throw an InvalidCastException if no match is
found.
'// You could use Regex.IsMatch or Integer.TryParse to check
'// the string value before casting into an integer.
Dim week As Integer = CInt(Regex.Match(dateString, "(?<Week>
\d{1,}(?=w))").Value)
Dim day As Integer = CInt(Regex.Match(dateString, "(?<Day>
\d{1,}(?=d))").Value)
Dim hour As Integer = CInt(Regex.Match(dateString, "(?<Hour>
\d{1,}(?=h))").Value)
Dim minute As Integer = CInt(Regex.Match(dateString, "(?
<Minute>\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
Dan
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
Dan
On Oct 6, 6:32 am, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
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.