PnD Posted April 18, 2021 Share Posted April 18, 2021 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? 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. Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted April 18, 2021 Share Posted April 18, 2021 @PnD StringSplit() returns a 1-Dimensional array, as stated in the help file, so you don't need to specify any other dimension. Just loop through the array with $i and you'd be able to achieve what you're trying to PnD 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Nine Posted April 18, 2021 Share Posted April 18, 2021 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) PnD 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
PnD Posted April 18, 2021 Author Share Posted April 18, 2021 (edited) Ah, I see now. No wonder why it never works in the first place. Thank you FrancescoDiMuro so much for pointing this out. Edited April 18, 2021 by PnD Link to comment Share on other sites More sharing options...
PnD Posted April 18, 2021 Author Share Posted April 18, 2021 (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 April 18, 2021 by PnD Link to comment Share on other sites More sharing options...
JockoDundee Posted April 18, 2021 Share Posted April 18, 2021 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. PnD 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Nine Posted April 18, 2021 Share Posted April 18, 2021 Unless you have Ave. or Ave, and having a loop to search for every possibilities will be costly (compare to the 1 liner SRE). PnD and JockoDundee 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
JockoDundee Posted April 18, 2021 Share Posted April 18, 2021 Good point. Though it was already getting clobbered performance-wise by the reg-ex PnD 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
PnD Posted April 19, 2021 Author Share Posted April 19, 2021 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 . Link to comment Share on other sites More sharing options...
PnD Posted April 19, 2021 Author Share Posted April 19, 2021 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! Link to comment Share on other sites More sharing options...
Nine Posted April 19, 2021 Share Posted April 19, 2021 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) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
PnD Posted April 20, 2021 Author Share Posted April 20, 2021 (edited) Thanks Nine! It works beautifully. Edited April 20, 2021 by PnD Link to comment Share on other sites More sharing options...
PnD Posted April 20, 2021 Author Share Posted April 20, 2021 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now