Jump to content
Sign in to follow this  
argumentum

[Solved] RegExp path

Recommended Posts

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

Share this post


Link to post
Share on other sites

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
Spoiler

(?i)C:\\Users\\[^\\]+\\AppData\\Local\\Temp\\.*

Use these options for the whole regular expression «(?i)»
   Case insensitive «i»
Match the character string “C:” literally (case insensitive) «C:»
Match the backslash character «\\»
Match the character string “Users” literally (case insensitive) «Users»
Match the backslash character «\\»
Match any character that is NOT the backslash character «[^\\]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the backslash character «\\»
Match the character string “AppData” literally (case insensitive) «AppData»
Match the backslash character «\\»
Match the character string “Local” literally (case insensitive) «Local»
Match the backslash character «\\»
Match the character string “Temp” literally (case insensitive) «Temp»
Match the backslash character «\\»
Match any single character that is NOT a line break character (line feed) «.*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

 

 

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

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

actually, given that regexp do more than 1 comparing at the time, I'd like to match more than one matching, like,

find: "C:\Users\Other\AppData\Local\Temp\~TEST~\"

in: "C:\Users\*\AppData\Local\Temp\*", "C:\anotherPath\*\subfolder\", etc.

if it matches any of the in return true

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Just now, argumentum said:

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

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...