Jump to content

Recommended Posts

Posted

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

 

Posted
  On 10/26/2020 at 3:02 PM, Jos said:

Something like this maybe:

$test = "P123456 2.pdf"
$test = StringRegExpReplace($test,".*[\s](.*)","$1")

Jos

Expand  

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.

 

Posted
  On 10/26/2020 at 3:11 PM, 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. ;) 

Expand  

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.

image.png.0fe83bfd7ce4980458540cd1cb20d4ed.png

  • Developers
Posted (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 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.
  :)

Posted
  On 10/26/2020 at 3:25 PM, 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

Expand  

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.

Posted (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 by jchd
Extra ) removed and \h better than \s in this case
  Reveal hidden contents

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 here
RegExp tutorial: enough to get started
PCRE 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)

Posted

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.

  Reveal hidden contents
Posted

 

  On 10/26/2020 at 4:32 PM, seadoggie01 said:

; Untested... i do bad with $iPos, so check it :)

 Local $sPartial = StringRight($sFileName, StringLen($sFileName) - $iPos + 1)

 

Expand  

@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

Posted (edited)
  On 10/26/2020 at 11:53 PM, 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

 

Expand  

Thanks alot! This one is easy to understand for me!

Edited by S_Auto

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...