Extracting numbers from a string


D

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?
 
Ad

Advertisements

H

Harry

Dan said:
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.....
 
D

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?
 
Ad

Advertisements

R

rowe_newsgroups

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
 
D

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.
 
N

Nick

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
 
C

Cor Ligthert[MVP]

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
 
Ad

Advertisements

Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top