S_Auto Posted October 26, 2020 Share Posted October 26, 2020 Hello All, I'm trying to figure out how to extract a string between for example a space and .xxx extention. Example "P123456 2.pdf" , extention could also be 4 characters so I stringtrim wont work. So result should just be "2". If filename would be "P123456.pdf" then result should be 0 if possible. I have following code below. I'm sure there needs to be put something where I for now put "INSERT SOMETHING HERE". I tried to figure out all these "strange symbols" in StringRegExp help file, but it looks like chinese to me. Could anyone set me on the correct path? Or is there some source where I can find a more clear/ newcomer friendly explanation about these? #include <File.au3> #include <Array.au3> #include <RecFileListToArray.au3> #include <String.au3> $DIR = "C:\Autoit\Projects\Scans\" MAP() Func MAP() $FileArray = _RecFileListToArray($DIR, "*", 1, 0, 1, 0) For $i = 1 To $FileArray[0] $FileArray[$i] = StringRegExpReplace($FileArray[$i], "INSERT SOMETHING HERE", "") ConsoleWrite($FileArray[$i] & @CRLF) Next EndFunc Link to comment Share on other sites More sharing options...
Developers Jos Posted October 26, 2020 Developers Share Posted October 26, 2020 Something like this maybe: $test = "P123456 2.pdf" $test = StringRegExpReplace($test,".*[\s](.*)","$1") Jos 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 More sharing options...
S_Auto Posted October 26, 2020 Author Share Posted October 26, 2020 4 minutes ago, Jos said: Something like this maybe: $test = "P123456 2.pdf" $test = StringRegExpReplace($test,".*[\s](.*)","$1") Jos Hey Jos, Thanks for the fast reply. This is however not giving me the expected result. Could you maybe help me by quickly describing how to read/ decipher the ".*[\s](.*)" part. It could help me out alot. Link to comment Share on other sites More sharing options...
Developers Jos Posted October 26, 2020 Developers Share Posted October 26, 2020 Which part isn't expected as this returns "2.pdf"? The explanation of the regex can be found in detail in the helpfile. 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 More sharing options...
S_Auto Posted October 26, 2020 Author Share Posted October 26, 2020 10 minutes ago, Jos said: Which part isn't expected as this returns "2.pdf"? The explanation of the regex can be found in detail in the helpfile. Hey Jos, See below results for me. I think I'm able to remove the .txt from the 2.txt, however see description again in first post. As described in my first post I have difficulties understanding the combining of these special pattern codes, thats why I asked. Link to comment Share on other sites More sharing options...
Developers Jos Posted October 26, 2020 Developers Share Posted October 26, 2020 (edited) Ok ... so simply add some logic to test whether there was a change.... something like this: $test = "P1234562.pdf" $testn = StringRegExpReplace($test,".*[\s](.*)","$1") If $test <> $testn then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $testn = ' & $testn & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console EndIf and when I am missing your point then please post the code containing an example as code and not as image, so I can test with that. Jos Edited October 26, 2020 by Jos 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 More sharing options...
S_Auto Posted October 26, 2020 Author Share Posted October 26, 2020 4 minutes ago, Jos said: Ok ... so simply add some logic to test whether there was a change.... something like this: $test = "P1234562.pdf" $testn = StringRegExpReplace($test,".*[\s](.*)","$1") If $test <> $testn then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $testn = ' & $testn & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console EndIf and when I am missing your point then please post the code containing an example as code and not as image, so I can test with that. Jos Thanks. So basically I also just figured out the symbols i mentioned are "regular expressions". I'll invest some time to study since this is new to me. I'll update the post if I have further questions. Link to comment Share on other sites More sharing options...
jchd Posted October 26, 2020 Share Posted October 26, 2020 (edited) $test = "P123456 2.pdf" $test = StringRegExpReplace($test,".*\h([^.]+)\.[^.]+$","$1") ConsoleWrite($test & @LF) Another skin for the same cat. Explanations here: https://regex101.com/r/KHP0xN/2 Edited October 26, 2020 by jchd Extra ) removed and \h better than \s in this case S_Auto 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
mikell Posted October 26, 2020 Share Posted October 26, 2020 1 hour ago, S_Auto said: So result should just be "2". If filename would be "P123456.pdf" then result should be 0 if possible. ? $test = "P123456 2.pdf" ;$test = "P1234562.pdf" $test = StringRegExpReplace($test,"(?|.*\h([^.]+)|.*)\.[^.]+$", "$1") $test = ($test == "") ? "0" : $test ConsoleWrite($test & @LF) Link to comment Share on other sites More sharing options...
seadoggie01 Posted October 26, 2020 Share Posted October 26, 2020 Seeing as this is such a simple pattern, you don't have to use regular expressions. Func FunkyFileName($sFileName) ; Untested... i do bad with $iPos, so check it :) ; Get the location of the space Local $iPos = StringInStr($sFileName, " ") ; No spaces? Return 0 If @error Then Return 0 ; Get everything to the right of the space Local $sPartial = StringRight($sFileName, StringLen($sFileName) - $iPos + 1) ; Get the postion of the period (beginning of the extension) $iPos = StringInStr($sPartial, ".") ; Weird... no extension If @error then Return $sPartial ; Return everything to the left of the extension Return StringLeft($sPartial, $iPos - 1) EndFunc All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
pseakins Posted October 26, 2020 Share Posted October 26, 2020 7 hours ago, seadoggie01 said: ; Untested... i do bad with $iPos, so check it Local $sPartial = StringRight($sFileName, StringLen($sFileName) - $iPos + 1) @seadoggie01 You don't need +1 to obtain partial in above example This is my solution, it takes care of longer extensions and multiple spaces in the filename and dispenses with confusing regex $sFileName = "P123456 2.pdf" $sPartial = StringLeft($sFileName, StringInStr($sFileName, ".") - 1) ; remove the extension $sResult = StringRight($sPartial, StringLen($sPartial) - StringInStr($sPartial, " ", 0, -1)) ; extract all to the right of the LAST space ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sResult=[' & $sResult & '] Error code: ' & @error & @CRLF) ;### Debug Console $sFileName = "P123456 2.pdfxy" $sPartial = StringLeft($sFileName, StringInStr($sFileName, ".") - 1) ; remove the extension $sResult = StringRight($sPartial, StringLen($sPartial) - StringInStr($sPartial, " ", 0, -1)) ; extract all to the right of the LAST space ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sResult=[' & $sResult & '] Error code: ' & @error & @CRLF) ;### Debug Console $sFileName = "P1 23456 2.pdf" $sPartial = StringLeft($sFileName, StringInStr($sFileName, ".") - 1) ; remove the extension $sResult = StringRight($sPartial, StringLen($sPartial) - StringInStr($sPartial, " ", 0, -1)) ; extract all to the right of the LAST space ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sResult=[' & $sResult & '] Error code: ' & @error & @CRLF) ;### Debug Console Phil Seakins Link to comment Share on other sites More sharing options...
S_Auto Posted October 28, 2020 Author Share Posted October 28, 2020 (edited) On 10/27/2020 at 12:53 AM, pseakins said: @seadoggie01 You don't need +1 to obtain partial in above example This is my solution, it takes care of longer extensions and multiple spaces in the filename and dispenses with confusing regex $sFileName = "P123456 2.pdf" $sPartial = StringLeft($sFileName, StringInStr($sFileName, ".") - 1) ; remove the extension $sResult = StringRight($sPartial, StringLen($sPartial) - StringInStr($sPartial, " ", 0, -1)) ; extract all to the right of the LAST space ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sResult=[' & $sResult & '] Error code: ' & @error & @CRLF) ;### Debug Console $sFileName = "P123456 2.pdfxy" $sPartial = StringLeft($sFileName, StringInStr($sFileName, ".") - 1) ; remove the extension $sResult = StringRight($sPartial, StringLen($sPartial) - StringInStr($sPartial, " ", 0, -1)) ; extract all to the right of the LAST space ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sResult=[' & $sResult & '] Error code: ' & @error & @CRLF) ;### Debug Console $sFileName = "P1 23456 2.pdf" $sPartial = StringLeft($sFileName, StringInStr($sFileName, ".") - 1) ; remove the extension $sResult = StringRight($sPartial, StringLen($sPartial) - StringInStr($sPartial, " ", 0, -1)) ; extract all to the right of the LAST space ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sResult=[' & $sResult & '] Error code: ' & @error & @CRLF) ;### Debug Console Thanks alot! This one is easy to understand for me! Edited October 28, 2020 by S_Auto 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