Jump to content

Recommended Posts

Posted

Dear all

Currently I can be able to compare single substring to a string and then I can be able to extract  "Suite 1234566" using the code below

$a="Ave"
$a1="1234 London Square Ave Suite 1234566"
Local $iPosition = StringInStr($a1,$a)
Local $iLength = StringLen($a1)

$newposition= $iPosition + stringlen($a)

local $a2= StringRight($a1,($iLength-$newposition))
MsgBox (0,0,$a2)

my question is that if i have a text file (file.txt) that have multiple substrings on it for $a, how would I implement it to the code above?

image.png.a520964737920319d8087f4e67806b18.png

I tried to assign $a to the $arr result by row, column

local $file= @ScriptDir & "\file.txt"
local $arr= stringsplit(fileread($file),@CRLF,3)

    For $i=0 to UBound($arr)
    $a=$arr[i][0]
    Next
    MsgBox(0,0,$a)

or even _arraytostring but still not working

For $i=0 to UBound($arr)
    $a= _ArrayToString($arr,i,UBound($arr))
    Next
    MsgBox(0,0,$a)

I really appreciate your feedback.

 

 

 

Posted

Using StringInStr can be misleading as you can search for AVE for example and the address can contain AVE somewhere else like 123 Maverick Road....

I think SRE would be more appropriate to perform a more robust search :

#include <Array.au3>

;Local $sList = StringReplace(FileRead("Text.txt"),@CRLF, "|")
Local $sList = "ALY|AVE|BCH|BLVD" ; for testing purpose
Local $sAdr = "1234 London Square Ave Suite 1234566"

Local $aExt = StringRegExp($sAdr, "(?i)\b(" & $sList & ")\b\h(.*)", 1)
_ArrayDisplay($aExt)

 

Posted (edited)
5 hours ago, Nine said:

Using StringInStr can be misleading as you can search for AVE for example and the address can contain AVE somewhere else like 123 Maverick Road....

I think SRE would be more appropriate to perform a more robust search :

#include <Array.au3>

;Local $sList = StringReplace(FileRead("Text.txt"),@CRLF, "|")
Local $sList = "ALY|AVE|BCH|BLVD" ; for testing purpose
Local $sAdr = "1234 London Square Ave Suite 1234566"

Local $aExt = StringRegExp($sAdr, "(?i)\b(" & $sList & ")\b\h(.*)", 1)
_ArrayDisplay($aExt)

 

Thank you so much for your solution. This works excellently and accurately and prevent false positive result if using Stringinstr

Edited by PnD
Posted
5 hours ago, Nine said:

Using StringInStr can be misleading as you can search for AVE for example and the address can contain AVE somewhere else

Perhaps searching for “ AVE “ would alleviate the problem.

Code hard, but don’t hard code...

Posted

Actually, I think JockoDundee works better for our case considering the situation "Ave. or Ave," that NINE mentioned above.

Unless we either use stringreplace to remove , and .  from $sAdr or add ave, and ave. to the  $Slist, I think stringinstr requires less condition for us when we just simply put extra space to $a

Here is my code and it works flawlessly

local $file= @ScriptDir & "\file.txt"
local $arr= stringsplit(fileread($file),@CRLF,3)

$address1 = "1234 London Squavere Ave, Suite 1234566"

    For $i=0 to UBound($arr)-1

    $a = $arr[$i]
    

    Local $iPosition = StringInStr($address1," "&$a)
    if $iPosition > 0 then
    Local $iLength = StringLen($address1)

    $newposition= $iPosition + stringlen($a)+1

    local $address2= StringRight($address1,($iLength-$newposition))
    endif

    Next
    MsgBox (0,0,$address2)

  The $address2 is always "Suite 1234566" regardless , or .

Posted

To be honest, It would just be the same. I do not think the performance is impact as the result show off pretty quickly for both ways.

Global $file= @ScriptDir & "\street.txt"
Global $sList = StringReplace(FileRead($file),@CRLF, "|")  ; Convert Enter to | example: "ALY|AVE|BCH|BLVD"
Global $sAdr1 = "1234 London Vencirtura CIR, Apt. #566999999"
Global $sAdr = StringReplace(StringReplace($sAdr1,",",""),".","")

If StringLen($sAdr)>= 35 Then

    Local $aExt = StringRegExp($sAdr, "(?i)\b(" & $sList & ")\b\h(.*)", 1)

    ;_ArrayDisplay($aExt)

    If UBound($aExt) = 0 Then
        MsgBox(0, "Contents", "Address is ok",0.5)
    else
    local $a2= $aExt[1]
    MsgBox(0,0,$a2)
    EndIf
EndIf

Thank you JockoDundee and Nine  again for your great help!

 

Posted
13 hours ago, Nine said:

Here the SRE that will skip dot and coma after AVE if there is any :

Local $aExt = StringRegExp($sAdr, "(?i)\b(" & $sList & ")\b\W*(.*)", 1)

 

Thanks Nine! It works beautifully.

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
  • Recently Browsing   0 members

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