473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Pulling specific words from a string

Okay, if anyone could toss me some idea's here, please bare with my
noobish questions, I just picked up VB2005 Pro about a week ago. ( no
prior VB at all )
Here's my issue..

I'm pulling information out of a text file and need to pull specific
words out of a string. For example, the text file looks sorta like
this:

[Thu Feb 16 04:43:30 2006] User1 purchased 1 Gumball for ( $100).
[Fri Feb 17 01:00:22 2006] User2 purchased 1 Box of Candy for ( $25).
[Fri Feb 17 18:57:39 2006] User3 purchased 3 Rolls of Paper for (
$100).

I have created text boxes such as date, buyer, item, qty., etc....
if I strip each line and break it into an array it seems to work fine
for populating what I need..
such as
txtBuyer.text = myArray(5)
txtQty.text = myArray(7)

but where I run into problems is the actual items themselves.. since
most of the items in the text file are more than one word, and vary
with each line, I'm sure I can't specify arrays like the other
information.
txt.Item = myArray(8) would work for the first line and populate the
box with "Gumball"
but if I use that for the next line, all I would get is.."Box"

Is there a way to search a string and pull information out from between
keywords ala "get all the words between 'purchased' and 'for' " ? - I
know this seems pretty stupid and I'm probably going about it TOTALLY
the wrong way.. but any feedback would be greatly appreciated.

Mar 3 '06 #1
5 2743

<ak**********@yahoo.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
[Thu Feb 16 04:43:30 2006] User1 purchased 1 Gumball for ( $100).
[Fri Feb 17 01:00:22 2006] User2 purchased 1 Box of Candy for ( $25).
[Fri Feb 17 18:57:39 2006] User3 purchased 3 Rolls of Paper for ( $100). Is there a way to search a string and pull information out from between
keywords ala "get all the words between 'purchased' and 'for' " ? - I
know this seems pretty stupid and I'm probably going about it TOTALLY
the wrong way.. but any feedback would be greatly appreciated.


Sub Srch()

Dim I As Integer, J As Integer, K As Integer

Dim S As String

S = "[Fri Feb 17 18:57:39 2006] User3 purchased 3 Rolls of Paper for (
$100)."

I = InStr(S, "purchased ")

J = InStr(S, "for ( ")

K = InStr(I + 10, S, " ")

Debug.Print(I.ToString & " : " & J.ToString & " : " & K.ToString)

End Sub


Mar 3 '06 #2
Given a split on space renders elements 0-7 as predictable, and the last 3
elements as predictable, all you really need to do is concatenate positions
8 through (ubound-3) back into 1 word again:

For intX as Integer = 8 to UBound(myArray) - 3
txtItemPurchased.text &= myArray(intX) & " "
Next

<ak**********@yahoo.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
Okay, if anyone could toss me some idea's here, please bare with my
noobish questions, I just picked up VB2005 Pro about a week ago. ( no
prior VB at all )
Here's my issue..

I'm pulling information out of a text file and need to pull specific
words out of a string. For example, the text file looks sorta like
this:

[Thu Feb 16 04:43:30 2006] User1 purchased 1 Gumball for ( $100).
[Fri Feb 17 01:00:22 2006] User2 purchased 1 Box of Candy for ( $25).
[Fri Feb 17 18:57:39 2006] User3 purchased 3 Rolls of Paper for (
$100).

I have created text boxes such as date, buyer, item, qty., etc....
if I strip each line and break it into an array it seems to work fine
for populating what I need..
such as
txtBuyer.text = myArray(5)
txtQty.text = myArray(7)

but where I run into problems is the actual items themselves.. since
most of the items in the text file are more than one word, and vary
with each line, I'm sure I can't specify arrays like the other
information.
txt.Item = myArray(8) would work for the first line and populate the
box with "Gumball"
but if I use that for the next line, all I would get is.."Box"

Is there a way to search a string and pull information out from between
keywords ala "get all the words between 'purchased' and 'for' " ? - I
know this seems pretty stupid and I'm probably going about it TOTALLY
the wrong way.. but any feedback would be greatly appreciated.

Mar 3 '06 #3
This should split the line for you.

Dim Source As String = "[Fri Feb 17 01:00:22 2006] User2
purchased 1 Box of Candy for ( $25)."
Dim i As Integer
Dim Time As String = ""
Dim User As String = ""
Dim Number As String = ""
Dim Item As String = ""
Dim Price As String = ""
Dim Words As Array

i = Source.IndexOf("]")
Time = Source.Substring(0, i + 1)

Source = Source.Substring(i + 1)
Words = Source.Split(" ")

User = Words(1)
Number = Words(3)
Price = Words(Words.Length - 1)
Price = Price.Substring(0, Price.Length - 2) ' remove
closing paren

For i = 5 To Words.Length - 4
Item &= " " & Words(i)
Next
Item = Item.TrimStart

Mar 3 '06 #4

<ak**********@yahoo.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
: Okay, if anyone could toss me some idea's here, please bare with my
: noobish questions, I just picked up VB2005 Pro about a week ago. ( no
: prior VB at all )
:
:
: Here's my issue..
:
: I'm pulling information out of a text file and need to pull specific
: words out of a string. For example, the text file looks sorta like
: this:
:
: [Thu Feb 16 04:43:30 2006] User1 purchased 1 Gumball for ( $100).
: [Fri Feb 17 01:00:22 2006] User2 purchased 1 Box of Candy for ( $25).
: [Fri Feb 17 18:57:39 2006] User3 purchased 3 Rolls of Paper for (
: $100).
:
: I have created text boxes such as date, buyer, item, qty., etc....
: if I strip each line and break it into an array it seems to work fine
: for populating what I need..
: such as
: txtBuyer.text = myArray(5)
: txtQty.text = myArray(7)
:
: but where I run into problems is the actual items themselves.. since
: most of the items in the text file are more than one word, and vary
: with each line, I'm sure I can't specify arrays like the other
: information.
: txt.Item = myArray(8) would work for the first line and populate the
: box with "Gumball"
: but if I use that for the next line, all I would get is.."Box"
:
: Is there a way to search a string and pull information out from between
: keywords ala "get all the words between 'purchased' and 'for' " ? - I
: know this seems pretty stupid and I'm probably going about it TOTALLY
: the wrong way.. but any feedback would be greatly appreciated.
'-------------------------------------
Option Strict

Imports Microsoft.VisualBasic
Imports System

Public Module [module]

Public Sub Main(Args() As String)
Dim S(2) As String
S(0) = "[Thu Feb 16 04:43:30 2006] User1 purchased 1 " & _
"Gumball for ( $100)."
S(1) = "[Fri Feb 17 01:00:22 2006] User2 purchased 1 " & _
"Box of Candy for ( $25)."
S(2) = "[Fri Feb 17 18:57:39 2006] User3 purchased 3 " & _
"Rolls of Paper for ( $100)."

For ndx as integer = 0 to 2
Console.WriteLine(s(ndx))
Console.WriteLine(GetPurchaseItem(s(ndx)) & vbCrLf)
Next
End Sub

Private Function GetPurchaseItem(s As String) As String
Dim StartOffset As Integer = Instr(s, "purchased") + 10
Dim EndOffset As Integer = Instr(s, "for (") - StartOffset - 1
Return Mid(s, StartOffset, EndOffset)
End Function

End Module
'-------------------------------------
Generates the following output:

'-------------------------------------

[Thu Feb 16 04:43:30 2006] User1 purchased 1 Gumball for ( $100).
1 Gumball

[Fri Feb 17 01:00:22 2006] User2 purchased 1 Box of Candy for ( $25).
1 Box of Candy

[Fri Feb 17 18:57:39 2006] User3 purchased 3 Rolls of Paper for ( $100).
3 Rolls of Paper

'-------------------------------------

Mar 3 '06 #5
This is exactly what regulare expressions are for. Use named groups (see
http://www.regular-expressions.info/dotnet.html for more info):

Dim input, pattern As String
Dim result As System.Text.RegularExpressions.Match

input = "[Fri Feb 17 18:57:39 2006] John Doe purchased 3 Rolls of Paper
for ($100)."
pattern = "\] (?<user>.*) purchased (?<goods>.*) for \(\$(?<price>.*)\)\."
result = System.Text.RegularExpressions.Regex.Match(input, pattern)

Dim user As String = result.Groups("user").Captures(0).Value
Dim goods As String = result.Groups("goods").Captures(0).Value
Dim price As String = result.Groups("price").Captures(0).Value

MsgBox("User = " & user & vbCrLf _
& "Goods = " & goods & vbCrLf _
& "Price = " & price)
--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Mar 3 '06 #6

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

Similar topics

2
by: PuckInLA | last post by:
I have a question. I have some data that I am pulling into a dataset that needs to have each row of data emailed out. I got the email funciton working great but its extracting that data that is...
2
by: Branden | last post by:
hi guys, i was wondering if it is possible to extract selected words in a field to be put in different fields automatically. Do i have to write the code in vb? This is what im trying to do....
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
1
by: Toska | last post by:
Hi Wonder how the should use the Regex members to add markup code to words in a text. Code: -------------------- string words1 = "dog|cat|pig|horse|hippo";
2
by: elyob | last post by:
Hi, I'm looking at pulling a string from a source file and am wondering the best way to do this. I have a starter and ender quote but am just wondering the best way to do this? Regular expressions?...
12
by: IamIan | last post by:
I searched the archives but couldn't find anyone else with this problem. Basically I'm grabbing all ASCII files in a directory and doing geoprocessing on them. I need to calculate a z-factor based...
26
by: Swroteb | last post by:
Hi there, I've got a reasonably sized list of objects that I'd like to pull out all combinations of five elements from. Right now I have a way to do this that's quite slow, but manageable. I...
12
by: Dixie | last post by:
Is there a way to shell to Microsoft Word from Access and load a specific template - using VBA? dixie
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.