Jump to content

Recommended Posts

Posted (edited)

I know nothing about RegExp. Too complicated for my brain cells :( 
All I need is to find, say:

C:\Users\Other\AppData\Local\Temp\~TEST~\ ( that I can do with StringInStr )
C:\Users\*\AppData\Local\Temp\* ( that I can NOT do with StringInStr )

I'll use this to skip matching paths ala StringInStr.  Thanks :)  

 

Answered at https://www.autoitscript.com/forum/topic/199287-solved-regexp-path/?do=findComment&comment=1429890

 

Edited by argumentum
Solved

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)

Something like this maybe?

#include <Constants.au3>
#include <Array.au3>

example()

Func example()
    ; Finds C:\Users\*\AppData\Local\Temp\*
    Const $kRegex = "(?i)C:\\Users\\[^\\]+\\AppData\\Local\\Temp\\.*"

    Local $sData
    $sData &= "C:\Users\Other\AppData\Local\Temp\~TEST~\" & @CRLF   ; Match
    $sData &= "C:\users\Joe\appdata\local\temp\test" & @CRLF        ; Match
    $sData &= "C:\Users\Mary\AppData\Local\Temp2\Test" & @CRLF      ; No match because "Temp2"

    Local $aResult = StringRegExp($sData, $kRegex, $STR_REGEXPARRAYGLOBALMATCH)
    _ArrayDisplay($aResult)
EndFunc
  Reveal hidden contents

 

Edited by TheXman
Made 2nd example lowercase to emphasize need to make regex case-insensitive
Posted (edited)

@FrancescoDiMuro, what @TheXman did but as a function returning true/false on the path as a string.

_IsThisInThePath("C:\Users\Other\AppData\Local\Temp\~TEST~\", "C:\Users\Other\AppData\Local\Temp\~TEST~\") ; that I can do with StringInStr
_IsThisInThePath("C:\Users\Other\AppData\Local\Temp\~TEST~\", "C:\Users\*\AppData\Local\Temp\*") ; that I can NOT do with StringInStr
func _IsThisInThePath($sFullPath, $sMatchThis)

    if $sFullPath match $sMatchThis via regular expresion    then
        Return True
    else
        Return False
    endIf

endFunc

or the like.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)

So something like this?

#include <Constants.au3>
#include <Array.au3>

Const $kRegex = "(?i)C:\\Users\\[^\\]+\\AppData\\Local\\Temp\\.*"

ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp\~TEST~\", $kRegex) & @CRLF)
ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp2\~TEST~\", $kRegex) & @CRLF)
ConsoleWrite(_IsThisInThePath("c:\users\mary\appdata\local\temp\~test~\", $kRegex) & @CRLF)

Func _IsThisInThePath($sPath, $sRegex)
    Return StringRegExp($sPath, $sRegex)
EndFunc

 

Edited by TheXman
Posted (edited)

So something like this maybe?

#include <Constants.au3>
#include <Array.au3>

ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp\~TEST~\", "C:\Users\*\AppData\Local\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp2\~TEST~\", "C:\users\*\appdata\local\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("c:\users\mary\appdata\local\temp\~test~\", "C:\Users\*\AppData\Local\Temp\") & @CRLF)

Func _IsThisInThePath($sPath, $sPattern)
    ; Convert plain text pattern to regular expression
    $sPattern = "(?i)" & $sPattern
    $sPattern = StringReplace($sPattern, "\", "\\")
    $sPattern = StringReplace($sPattern, "*", "[^\\]+")

    Return StringRegExp($sPath, $sPattern)
EndFunc

 

Edited by TheXman
Posted
  On 6/16/2019 at 6:38 PM, argumentum said:

Now, can I do more than 1 RegExp in the matching, or is it one by one only ?

Expand  

Yes, the possibilities are endless. :)  It just depends on your imagination and ability to implement it.

Posted (edited)

You're welcome!   :thumbsup:

Here's a little quick & dirty implementation of one way to pass multiple patterns.  Basically, I created it so that you separate multiple patterns with "|".  So you can have as many as you'd like.  Also, as you can see, I didn't add any error checking, which it certainly would need. :)

#include <Constants.au3>
#include <Array.au3>

ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp\~TEST~\", "C:\Users\*\AppData\Local\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp\~TEST~\", "C:\users\*\appdata\*\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp2\~TEST~\", "C:\users\*\appdata\local\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("c:\users\mary\appdata\local\temp\~test~\", "C:\Users\*\AppData\Local\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("C:\abc\def\ghi\Local\Temp\~TEST~\", "C:\users\*\appdata\*\Temp\|C:\abc\*\ghi\") & @CRLF) ;Multiple patterns


Func _IsThisInThePath($sPath, $sPattern)
    Local $bPathFound = False
    Local $aPatterns
    Local $sRegex

    ; Split multiple patterns into an array of patterns and
    ; spin thru patterns to see if any match.
    $aPatterns = StringSplit($sPattern, "|")
    For $i = 1 To $aPatterns[0]
        $sRegex = $aPatterns[$i]
        $sRegex = "(?i)" & $sRegex
        $sRegex = StringReplace($sRegex, "\", "\\")
        $sRegex = StringReplace($sRegex, "*", "[^\\]+")

        If StringRegExp($sPath, $sRegex) Then $bPathFound = True
    Next

    Return $bPathFound
EndFunc

Or my previous function would work too since the "|" character is the regex way of having multiple patterns. :)

#include <Constants.au3>
#include <Array.au3>

ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp\~TEST~\", "C:\Users\*\AppData\Local\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp\~TEST~\", "C:\users\*\appdata\*\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("C:\Users\Other\AppData\Local\Temp2\~TEST~\", "C:\users\*\appdata\local\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("c:\users\mary\appdata\local\temp\~test~\", "C:\Users\*\AppData\Local\Temp\") & @CRLF)
ConsoleWrite(_IsThisInThePath("C:\abc\def\ghi\Local\Temp\~TEST~\", "C:\users\*\appdata\*\Temp\|C:\abc\*\ghi\") & @CRLF) ;Multiple patterns


Func _IsThisInThePath($sPath, $sPattern)
    ; Convert plain text pattern to regular expression
    $sPattern = "(?i)" & $sPattern
    $sPattern = StringReplace($sPattern, "\", "\\")
    $sPattern = StringReplace($sPattern, "*", "[^\\]+")

    Return StringRegExp($sPath, $sPattern)
EndFunc

 

Edited by TheXman

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