Jump to content

Get filename Regex quotation mark and slash issue


youtuber
 Share

Recommended Posts

What's the condition to not match myprogram as well?

If it's for getting program names w/o extension in this sample, use (?i)["\\]([^\\]*)\.exe"

Explain your complete requirements in full detail please.

Edited by jchd

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)

Link to comment
Share on other sites

Again, what's the condition to NOT match the other(s) .exe ???

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)

Link to comment
Share on other sites

@jchd 

1 hour ago, jchd said:

(?i)["\\]([^\\]*)\.exe"

 

(?i)["\\]([^ \\]*)\.exe" ; Here is exactly the condition I want when there is one empty, an interesting empty :huh:

https://regex101.com/r/BL19yc/2

Yes, this works fine for me, thanks, but can it cause problems for a more specific mess?

 

Edited by youtuber
Link to comment
Share on other sites

Please be more specific! Your question is way too vague.

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)

Link to comment
Share on other sites

1 minute ago, youtuber said:

(?=.exe)\.\w{2,3}

There is redundancy here: you ask for the string .exe to follow the matchpoint and then you want it to match the pattern \.\w{2,3}

It's plain that .exe matches \.\w{2,3}

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)

Link to comment
Share on other sites

You get the same matches with (?i)["\\]([^ \\]+).exe"

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)

Link to comment
Share on other sites

Do you encounter empty program names often, like "C:\chkfhi\.exe" ?

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)

Link to comment
Share on other sites

"\\?([\w ]+)(?=\.exe)

This should also work (with the following assumptions):

  • file name will always be enclosed in double quotes
  • file name will consist of only letters, number, underscores, and spaces
  • file name will always end with .exe
  • file name optionally begins with a "\" directly after the initial double quote

I left off the (?i) for case-insensitivity.  You can add it back if it's needed.

https://regex101.com/r/xYqThi/1

 

Edited by TheXman
Link to comment
Share on other sites

That makes an assumption that the file name is not in a subdirectory as something like this in the last line is not captured

ShellExecute(@HomeDrive & "\Program Files\PROGRAMDIR\Program Dir\my program.exe", @ScriptDir & "\test\Testblabla.exe")

And the previous lot are equivalent to just acting on an exe with no spaces in the name

(?:\\|")\w*.exe

 

i would lean towards the trailing comma as the criteria to exclude "my program.exe" from the example.  In both the first Send command and the last Shellexecute,  the exe is in the last parameter.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Here is a reply to your questions.

#include <Array.au3>

#cs
Send('BLA.CMD "Testasdf.exe" folder\file.txt asdf123')
Send('{enter}')
ShellExecute(@ScriptDir & "\folder\file.txt")

ShellExecute(@HomeDrive & "\Program Files\PROGRAMDIR\Program Dir\my program.exe", @ScriptDir & "\Testblabla.exe")
#ce

Local $sTestString = StringRegExpReplace(FileRead(@ScriptFullPath), "(?s)^.*?#cs\v+|\v+#ce.*$", "")
; ConsoleWrite($sTestString & @CRLF)

$a1 = StringRegExp($sTestString, '(?i)[\w\h]+(?=\.exe"(?:\h|\)))', 3) ; post #1, after results Testasdf and Testblabla
_ArrayDisplay($a1)

$a2 = StringRegExp($sTestString, '(?i)[\w\h]+\.exe(?="(?:\h|\)))', 3) ; post #3, after results Testasdf.exe and Testblabla.exe
_ArrayDisplay($a2)

ConsoleWrite(StringRegExpReplace($sTestString, '(?is).*?(?<= "|"\\)([\w\h]+)(\.exe"\h|\.exe"\)).*?', "\1" & ' ') & @CRLF)  ; post #1, after results Testasdf and Testblabla
ConsoleWrite("------------------" & @CRLF)
ConsoleWrite(StringRegExpReplace($sTestString, '(?is).*?(?<= "|"\\)([\w\h]+\.exe)("\h|"\)).*?', "\1" & @CRLF))   ; post #3, after results Testasdf.exe and Testblabla.exe

#cs ; Returns @ output console:-
Testasdf Testblabla
------------------
Testasdf.exe
Testblabla.exe
#ce

 

Link to comment
Share on other sites

8 hours ago, iamtheky said:

i would lean towards the trailing comma as the criteria to exclude "my program.exe" from the example

I agree ... so this should work

#Include <Array.au3>

$txt = "Send('BLA.CMD ""Testasdf.exe"" folder\file.txt asdf123')" & @crlf & _ 
    "Send('{enter}')" & @crlf & _ 
    "ShellExecute(@ScriptDir & ""\folder\file.txt"")" & @crlf & _ 
    "ShellExecute(@HomeDrive & ""\Program Files\PROGRAMDIR\Program Dir\my program.exe"", @ScriptDir & ""\Testblabla.exe"")"

$res = StringRegExp($txt, '(\w+)\.exe(?!.*?,)', 3)
_ArrayDisplay($res)

 

Link to comment
Share on other sites

@mikell Your sample pattern doesn't work for this :(

#Include <Array.au3>

$txt = "Send('BLA.CMD ""Testasdf!^+%&()=_.exe"" folder\file.txt asdf123')" & @crlf & _
    "Send('{enter}')" & @crlf & _
    "ShellExecute(@ScriptDir & ""\folder\file.txt"")" & @crlf & _
    "ShellExecute(@HomeDrive & ""\Program Files\PROGRAMDIR\Program Dir\my program.exe"", @ScriptDir & ""\Testblabla!^+%&()=_.exe"")"

$res = StringRegExpReplace($txt, '(\w+)\.exe(?!.*?,)', 'Replacewith.exe')
MsgBox(0,"", $res)

 

I will use it for this

#include <File.au3>

Local $FileSearch = _FileListToArray(@ScriptDir & "\", "*.exe", 1)
If Not @error Then
    For $i = 1 To $FileSearch[0]
        _ChangeEXEnameINau3($FileSearch[$i])
    Next
EndIf

Func _ChangeEXEnameINau3($aFileNameExe)
    Local $aFileList = _FileListToArray(@ScriptDir & "\MyprojectAu3\", "*.au3", 1)
    If Not @error Then
        For $i = 1 To UBound($aFileList) - 1
            $Au3_path = @ScriptDir & "\MyprojectAu3\" & $aFileList[$i]
            $Read = FileRead($Au3_path)
            $sRegexRep = StringRegExpReplace($Read, '(\w+)\.exe(?!.*?,)', $aFileNameExe)
            $Open = FileOpen($Au3_path, 2)
            FileWrite($Open, $sRegexRep)
            FileClose($Open)
        Next
    EndIf
EndFunc   ;==>_ChangeEXEnameINau3
Edited by youtuber
Link to comment
Share on other sites

54 minutes ago, youtuber said:

Your sample pattern doesn't work for this

It's because the file names contain non-word characters. Just replace in the expression \w  by  [^"]  :)

#Include <Array.au3>

$txt = "Send('BLA.CMD ""Testasdf!^+%&()=_.exe"" folder\file.txt asdf123')" & @crlf & _
    "Send('{enter}')" & @crlf & _
    "ShellExecute(@ScriptDir & ""\folder\file.txt"")" & @crlf & _
    "ShellExecute(@HomeDrive & ""\Program Files\PROGRAMDIR\Program Dir\my program.exe"", @ScriptDir & ""\Testblabla!^+%&()=_.exe"")"

$res = StringRegExpReplace($txt, '([^"]+)\.exe(?!.*?,)', 'Replacewith.exe')
MsgBox(0,"", $res)

 

Link to comment
Share on other sites

Hmm. jchd, was right, you must strictly define your requirements

Currently it seems that the best way is the concept from Malkey

#Include <Array.au3>

$txt = "Send('BLA.CMD ""Testasdf!^+%&()=_.exe"" folder\file.txt asdf123', 1)" & @crlf & _
    "Send('{enter}')" & @crlf & _
    "ShellExecute(@ScriptDir & ""\folder\file.txt"")" & @crlf & _
    "ShellExecute(@HomeDrive & ""\Program Files\PROGRAMDIR\Program Dir\my program.exe"", @ScriptDir & ""\Testblabla!^+%&()=_.exe"")"

$res = StringRegExpReplace($txt, '[^"]+(?=\.exe"(?:\h|\)))', 'Replacewith.exe')
MsgBox(0,"", $res)

 

Link to comment
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
 Share

  • Recently Browsing   0 members

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