Sign in to follow this  
Followers 0
gcue

which function to capture string

34 posts in this topic

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!

Share this post


Link to post
Share on other sites



#22 ·  Posted (edited)

nevermind im good to go.. thanks bud.

Edited by gcue

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#25 ·  Posted (edited)

You need to learn regular expressions because you obviously do not know what you're doing. This is a good site: http://www.regular-expressions.info/tutorial.html. Your expressions should be:

$nfname = StringRegExp($string,"(?:Given Name:.)(.*)\r\n",3)
$nlname = StringRegExp($string,"(?:Last Name:.)(.*)\r\n",3)
Edited by zorphnog

Share this post


Link to post
Share on other sites

#26 ·  Posted (edited)

#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

Share this post


Link to post
Share on other sites

works great.

except im getting some spaces on one of the returned values

" jones"

"bob "

any way to get rid of those?

Share this post


Link to post
Share on other sites

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.

:)


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Doesn't do it for me. The spaces must be in your text file. I get a match between both files.

Share this post


Link to post
Share on other sites

#30 ·  Posted (edited)

Take the tutorial! http://www.regular-expressions.info/tutorial.html

$array = StringRegExp($Original,"(?:(?:Given Name|Last Name):\s+)(.*)\r\n",3)
Edited by zorphnog

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Well it works for me, unless you have some strange whitespace character. But as @Jos pointed out, there is a simple string function you can use to clear this up. :)

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

got it working perfectly!!

thanks a bunch fellas.. the tutorial def helped.

Take the tutorial! http://www.regular-expressions.info/tutorial.html

$array = StringRegExp($Original,"(?:(?:Given Name|Last Name):\s+)(.*)\r\n",3)

Share this post


Link to post
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
Sign in to follow this  
Followers 0