Jump to content

which function to capture string


Recommended Posts

Dude! It doesn't matter what the name is. The expression retrieves wahtever follows "Full name".

Do you see this part?

;Delete the following lines when you're done testing

$String = ""

$String &= "This is a line" & @CRLF

$String &= "Full Name Jones, Bob" & @CRLF

$String &= "This is another line" & @CRLF

These 4 lines are just to simulate FileRead!

Link to comment
Share on other sites

ya i had done that - but id get an array error.. so i putem back

"subscript used with non-array variable"

hahah

#include <file.au3>

;Search folder
$root = @DesktopDir & "\"

;Grab list of txt files
$FileList = _FileListToArray($root,"*.txt")

;Loop through text files
For $X = 1 to $FileList[0]
    ;Combine filename with source folder
    $FileName = $root & $FileList[$X]
    
    ;Read file contents
    $String = FileRead($FileName)
    
    ;Find name
    $array = StringRegExp($string,"(?:Full Name.)(.*),.(.*)\r\n",3)
    
    ;If name found, display it
    If IsArray($array) Then
        ;Firstname Lastname
        MsgBox(0,"",$array[1] & " " & $array[0])
        
        ;Dump array to console
        For $X = 0 to Ubound($array)-1
            ConsoleWrite($array[$X] & @CRLF)
        Next
    EndIf
Next
Link to comment
Share on other sites

ok works fantastic

but now im trying to compare that value from another file for the same person

but in the other file the fields are

Given Name: Firstname

Last Name: Lastname

instead of the first file where it was

Full Name Lastname, Firstname

so i have this (but it doesnt capture the whole firstname nor whole lastname

$String = FileRead($file)

$nfname = StringRegExp($string,"(?:Given Name:.)(.*).(.*)\r\n",3)

MsgBox(0,"",$nfname[0])

$nlname = StringRegExp($string,"(?:Last Name:.)(.*).(.*)\r\n",3)

MsgBox(0,"",$nlname[0])

#include <file.au3>

;Search folder
$root = @DesktopDir & "\"

;Grab list of txt files
$FileList = _FileListToArray($root,"*.txt")

;Loop through text files
For $X = 1 to $FileList[0]
    ;Combine filename with source folder
    $FileName = $root & $FileList[$X]
    
    ;Read file contents
    $String = FileRead($FileName)
    
    ;Find name
    $array = StringRegExp($string,"(?:Full Name.)(.*),.(.*)\r\n",3)
    
    ;If name found, display it
    If IsArray($array) Then
        ;Firstname Lastname
        MsgBox(0,"",$array[1] & " " & $array[0])
        
        ;Dump array to console
        For $X = 0 to Ubound($array)-1
            ConsoleWrite($array[$X] & @CRLF)
        Next
    EndIf
Next
Link to comment
Share on other sites

#include <file.au3>

Dim $Name1 = "", $Name2 = ""

$Original = FileRead("someotherfile.txt")
$array = StringRegExp($Original,"(?:(?:Given Name|Last Name):.)(.*)\r\n",3)
;If name found, display it
If IsArray($array) Then
    $Name1 = $array[1] & " " & $array[0]
    
   ;Dump array to console
    For $X = 0 to Ubound($array)-1
        ConsoleWrite($array[$X] & @CRLF)
    Next
EndIf


;Search folder
$root = @DesktopDir

;Grab list of txt files
$FileList = _FileListToArray($root,"*.txt")

;Loop through text files
For $X = 1 to $FileList[0]
   ;Combine filename with source folder
    $FileName = $root & "\" & $FileList[$X]
    
   ;Read file contents
    $String = FileRead($FileName)
    
   ;Find name
    $array = StringRegExp($string,"(?:Full Name.)(.*),.(.*)\r\n",3)
    
   ;If name found, display it
    If IsArray($array) Then
       ;Firstname Lastname
        $Name2 = $array[1] & " " & $array[0]
        
        If $Name2 = $Name1 Then
            MsgBox(0,"Match","Name1: " & $Name1 & @CRLF & "Name2: " &$Name2)
        Else
            MsgBox(0,"No Match","Name1: " & $Name1 & @CRLF & "Name2: " &$Name2)
        EndIf
        
       ;Dump array to console
        For $X = 0 to Ubound($array)-1
            ConsoleWrite($array[$X] & @CRLF)
        Next
    EndIf
Next

Edited by weaponx
Link to comment
Share on other sites

  • Developers

works great.

except im getting some spaces on one of the returned values

" jones"

"bob "

any way to get rid of those?

Did you check the String functions in the helpfile ?

I am sure you can figure this one out yourself.

:)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

zorph: im going through the tutorial - thanks =)

btw, zorp, i tried ur example and it didnt work either.

weapon: yep it is in the text file.. so im trying to get rid of them

Link to comment
Share on other sites

Ok I forgot to take into account whitespace at the end of the name and before the line break. So this would be more fool proof:

$array = StringRegExp($Original,"(?:(?:Given Name|Last Name):\s+)([A-Za-z]+).*\r\n",3)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...