Pulling specific words from a string

  • Thread starter Thread starter akelly_image
  • Start date Start date
A

akelly_image

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.
 
[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
 
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
 
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
 
: 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

'-------------------------------------
 
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)
 
Back
Top