Windows Vista Forums

Looking for a name within a string
  1. #1


    Bill Guest

    Looking for a name within a string

    Hello

    I'm looping over a file ,and as I get each line I'm looking for a particualr
    name. I've tried use the split method, but each line contains whitespaces,
    and an un even number of white spaces, so the text item I'm looking for
    isn't always th esame element.

    Here is a sample of each line of text I I get

    devicetype1 address1 sn1 file1.txt cost1
    devicetype2 address2 sn2 file2.txt cost2

    The file I'm looking for has the .txt extension it, so how would I go about
    finding the location, and going backwards till the firs twhite space??



    Thanks



      My System SpecsSystem Spec

  2. #2


    Pegasus [MVP] Guest

    Re: Looking for a name within a string



    "Bill" <someplace@newsgroup> wrote in message
    news:u43FsDi7KHA.5412@newsgroup

    > Hello
    >
    > I'm looping over a file ,and as I get each line I'm looking for a
    > particualr name. I've tried use the split method, but each line contains
    > whitespaces, and an un even number of white spaces, so the text item I'm
    > looking for isn't always th esame element.
    >
    > Here is a sample of each line of text I I get
    >
    > devicetype1 address1 sn1 file1.txt cost1
    > devicetype2 address2 sn2 file2.txt cost2
    >
    > The file I'm looking for has the .txt extension it, so how would I go
    > about finding the location, and going backwards till the firs twhite
    > space??
    >
    > Thanks
    To find the .txt string, use the InStr method. This allows you to turn the
    string
    devicetype1 address1 sn1 file1.txt cost1
    into this one:
    devicetype1 address1 sn1 file1.txt
    You now use the InStrRev method to find the right-most space. Both methods
    are described in detail in the downloadable file script56.chm, with actual
    examples.


      My System SpecsSystem Spec

  3. #3


    Steve Guest

    Re: Looking for a name within a string

    Bill wrote:

    >
    > I'm looping over a file ,and as I get each line I'm looking for a
    > particualr name. I've tried use the split method, but each line
    > contains whitespaces, and an un even number of white spaces, so the
    > text item I'm looking for isn't always th esame element.
    >
    > Here is a sample of each line of text I I get
    >
    > devicetype1 address1 sn1 file1.txt cost1
    > devicetype2 address2 sn2 file2.txt cost2
    >
    > The file I'm looking for has the .txt extension it, so how would I go
    > about finding the location, and going backwards till the firs twhite
    > space??
    I use regular expressions to parse free-form strings:

    ===

    ' use array to mimic file input
    input = Array( _
    "devicetype1 address1 sn1 file1.txt cost1", _
    "devicetype2 address2 sn2 file2.txt cost2" _
    )

    Set parser = New RegExp
    parser.Pattern = "\S+\.txt\b"
    parser.IgnoreCase = True

    For Each line In input
    For Each match In parser.Execute(line) ' there will only be 1 match
    ' process file name here
    WScript.Echo match
    Next
    Next

    ===

    --
    Steve

    America is the only country that went from barbarism to decadence
    without civilization in between. -Oscar Wilde






      My System SpecsSystem Spec

Looking for a name within a string problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Find a string within a variable string Edward.A.Gonzalez PowerShell 4 15 Aug 2008
WCF converts List<string> to string[] John WinFX General 3 17 Jul 2008
problems with $var | select-string -pattern $string -q Ben Christian PowerShell 3 08 Feb 2008
How export-csv deals with string versus string[] Marco Shaw PowerShell 2 13 Jul 2007
String PRODUCT_NAME was not found in string table Extracampine Vista General 3 12 Feb 2007