473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting numbers from a string

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
10 4076
"Dan" <da**********@n ewcross-nursing.comschr ieb
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
"Dan" <da**********@n ewcross-nursing.comwrot e in message
news:11******** **************@ o80g2000hse.goo glegroups.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
I'd look into regular expressions.

Good Luck, Bob

"Dan" <da**********@n ewcross-nursing.comwrot e in message
news:11******** **************@ o80g2000hse.goo glegroups.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
Dan,

Like Harry I would just simple use the split for this.

Cor
Oct 5 '07 #5
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
On Oct 5, 6:33 am, Dan <dan_willi...@n ewcross-nursing.comwrot e:
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.Reg ularExpressions

Module Module1

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

'// These will throw an InvalidCastExce ption if no match is
found.
'// You could use Regex.IsMatch or Integer.TryPars e to check
'// the string value before casting into an integer.
Dim week As Integer = CInt(Regex.Matc h(dateString, "(?<Week>
\d{1,}(?=w))"). Value)
Dim day As Integer = CInt(Regex.Matc h(dateString, "(?<Day>
\d{1,}(?=d))"). Value)
Dim hour As Integer = CInt(Regex.Matc h(dateString, "(?<Hour>
\d{1,}(?=h))"). Value)
Dim minute As Integer = CInt(Regex.Matc h(dateString, "(?
<Minute>\d{1,}( ?=m))").Value)

Console.WriteLi ne("The Week is {0}", week.ToString() )
Console.WriteLi ne("The Day is {0}", day.ToString())
Console.WriteLi ne("The Hour is {0}", hour.ToString() )
Console.WriteLi ne("The Minute is {0}", minute.ToString ())

Console.Read()
End Sub

End Module
/////////////////////////

Thanks,

Seth Rowe

Oct 5 '07 #7
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
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("We eks=" & myTimetable("We eks").ToString _

& " Days=" & myTimetable("Da ys").ToString _

& " Hours=" & myTimetable("Ho urs").ToString _

& " Minutes=" & myTimetable("Mi nutes").ToStrin g)

Function ParseTimeString (ByVal inHashtable As Hashtable, ByVal inString As
String) As Hashtable

Dim myRegex As System.Text.Reg ularExpressions .Regex

Dim myMatch As System.Text.Reg ularExpressions .Match

Dim myDictionaryEnt ry As DictionaryEntry

Dim Timetable As New Hashtable

For Each myDictionaryEnt ry In inHashtable

Dim myPattern As String

myPattern = "(?<" & myDictionaryEnt ry.Key & ">\d{1,}(?= " &
myDictionaryEnt ry.Value & "))"

myRegex = New System.Text.Reg ularExpressions .Regex(myPatter n)

myMatch = myRegex.Match(i nString)

If myMatch.Success = True Then

Timetable.Add(m yDictionaryEntr y.Key,
myMatch.Groups( myDictionaryEnt ry.Key).Value)

Else

Timetable.Add(m yDictionaryEntr y.Key, vbNull)

End If

Next

Return Timetable

End Function
Oct 5 '07 #9
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6955
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract some part of it like all names or numbers from each line, simply text fom between e.g. second and third colon. And turn it
5
2956
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past, I have used individual entry fields for each variable. I would now like to use text area boxes to simplify the data entry (this way, data can be produced by another program--FORTRAN, "C", etc.--but analyzed online, so long as it is first...
7
3002
by: Raphi | last post by:
Hi, I'm trying to clean up a large database in Access. I have one field for address, which needs to be broken up into Street Number, Street Name, and Street Label (St., Road, etc.) The problem is that the data is very dirty. So some addresses will be standard "456 XYZ Road," while others won't have a number and will just say "XYZ Industrial Park," meaning I can't just use Instr to search for the first space because sometimes the...
13
15980
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The reason I ask is that I am programming on a RISC system where division is quite expensive, and I want to be able to extract the digits from an integer (the integer will be from 1 to 6 digits long, and I know how many digits are in the number).
5
4175
by: Jensen bredal | last post by:
Hello, I have a string formated in the following way: s = 1;32;100;32;09;.........;09;76; I need to extract the numbers separated by the seicolon. The list can contain several thousands of items and the code is time critical. How can i best extract them in C#.
6
1851
by: RSH | last post by:
Hi, I have quite a few .DAT data files that i need to extract the data out of. When i open the files in a text editor I see all of the text that I need to get at BUT there are a lot of junk (binary?) characters and white space in non logical formatting positions. Here is a small sample of what the data looks like: 0~ 0501101010512505011132451235 >   ô ô
4
2570
by: dor | last post by:
i have an input file named input.txt where all the data looks like this: (4,10) 20 (5,3) 13 (7,19) 6 .. .. .. the numbers are random. i need to use every number in each line
16
10975
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some pointers where to get started would be much appreciated. An example text file: ----------- Some text that can span some lines.
7
3456
by: asedt | last post by:
I have strings like, only some examples: ABC 213/23213,23 ABC 213/23213,DSF CVNCVB 3456/324 XCVV 123 /234/324 I need to take out the two first numbers.
7
31090
by: iheartvba | last post by:
I am trying to extract numbers from a string in the access query builder, all I need is the position of the numbers so the instr function will do. However I need a code like InStr(.,"") where ] "" can be any number from 0 - 9. I can't figure out what to use as a wild card for numbers. Please help Thank You
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.