Jump to content

StringRegExp and path


Recommended Posts

Hello, I need help on the command StringRegExp.

I should be recognized by a path...

Example:

String: Hi, C:\WINDOWS\explorer.exe -> Return: C:\WINDOWS\exploder.exe

String: C:\WINDOWS\system32\shell32.dll -> Return: C:\WINDOWS\system32\shell32.dll

String: C:\ -> Null

String: C:\WINDOWS\ -> Null

String: Hi, run C:\WINDOWS\svchost.exe -> Return: C:\WINDOWS\svchost.exe

How do I? Thanks for ur help and sorry for my English =(

Link to comment
Share on other sites

  • Moderators

Snoomi,

Welcome to the AutoIt forum. :mellow:

I am sure some SRE guru will come along with a one-liner in a minute, but until then this seems to work: :)

$sString = "Hi, C:\WINDOWS\explorer.exe" ; -> Return: C:\WINDOWS\exploder.exe
ConsoleWrite(_Extract($sString) & @CRLF)

$sString = "C:\WINDOWS\system32\shell32.dll" ; -> Return: C:\WINDOWS\system32\shell32.dll
ConsoleWrite(_Extract($sString) & @CRLF)

$sString = "C:\" ; -> Return: Null
ConsoleWrite(_Extract($sString) & @CRLF)

$sString = "C:\WINDOWS\" ; -> Return: Null
ConsoleWrite(_Extract($sString) & @CRLF)

$sString = "Hi, run C:\WINDOWS\svchost.exe" ; -> Return: C:\WINDOWS\svchost.exe
ConsoleWrite(_Extract($sString) & @CRLF)

Func _Extract($sString)

    ; Extract the file name
    $sFile = StringRegExpReplace($sString, "^.*\\", "")
    ; If no file then Null
    If $sFile = "" Then Return "Null"
    ; Look for this path
    If StringInStr($sString, "C:\WINDOWS\system32\" & $sFile) Then
        Return "C:\WINDOWS\system32\" & $sFile
    ; Or this one
    If StringInStr($sString, "C:\WINDOWS\" & $sFile) Then
        Return "C:\WINDOWS\" & $sFile
    ; And if neither were found
    Else
        Return "Null"
    EndIf

EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Snoomi,

This should pull all the C:\WINDOWS\ paths from a string: :)

$sString = "String: Hi, C:\WINDOWS\explorer.exe -> Return: C:\WINDOWS\exploder.exe" & @CRLF & _
           "String: C:\WINDOWS\system32\shell32.dll -> Return: C:\WINDOWS\system32\shell32.dll" & @CRLF & _
           "String: C:\ -> Null" & @CRLF & _
           "String: C:\WINDOWS\ -> Null" & @CRLF & _
           "String: Hi, run C:\WINDOWS\svchost.exe -> Return: C:\WINDOWS\svchost.exe"

; get an array of all the matches
$aRet = StringRegExp($sString, "(?i)(?U)C:\\WINDOWS\\(.*)\x20", 3)

; Show the ones that match
For $i = 0 To UBound($aRet) - 1
    If $aRet[$i] Then ConsoleWrite("C:\WINDOWS\" & $aRet[$i] & @CRLF)
Next

SRE explanation:

(?i)          - Case insensitive
(?U)          - Smallest match
C:\\WINDOWS\\ - Look for C:\WINDOWS\
(.*)          - Capture the text we meet before...
\x20          - The first space

Using flag 3 we get an array of the matches which we then run through and reattach the C:\WINDOWS bit if we found a path with something after that bit.

All clear? :mellow:

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Is very close to what I'm looking for.

However, the path could be anything! Not only C:\ WINDOWS, also C: \Temp to say... but even just a drive, such as E:, with a file.

I find Drive + File + Extension or Drive + Directory (even more than one) + File + Extension.

To be clear... should recognize ANY type of path.

Link to comment
Share on other sites

  • Moderators

Snoomi,

If you want help here it really helps if you explain what you want clearly to begin with - then we do not our waste time providing you with code which you do not want. :mellow:

This code will pull all paths in a text:

#include <Array.au3>

$sString = "blah blah blah blah C:\fred\fred.ext blah blah blah blah D:\tom\dick\harry blah blah blah blah E:\ blah blah blah blah"

$aRet = StringRegExp($sString, "(?i)(?U)[[:alpha:]]:.*\x20", 3)

_ArrayDisplay($aRet)

SRE explanation:

(?i)(?U)            - As before
[[:alpha:]]:.*\x20  - Look for a letter followed by a colon and then all characters up to the next space

Is that what you want? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

That's close but not accurate and I don't have time today to mess with it.

Spaces are legal in a path

"C:\freds files\fred.ext" would fail.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

Snoomi,

George is, as usual, correct when it comes to SREs. :)

See if this one is any better:

#include <Array.au3>

$sString = "blah blah blah blah C:\fred path\fred.ext blah blah blah blah D:\tom\dick\harry.dll blah blah blah blah E:\ blah blah blah blah"

$aRet = StringRegExp($sString, "(?U)[[:alpha:]]:\\.*\..*\x20" , 3)

_ArrayDisplay($aRet)

SRE:

(?U)            - Smallest match
[[:alpha:]]:\\  - Match a letter followed by :\
.*\.            - Followed by any number of characters until you find a "."
.*\x20          - Followed by any number of characters until you find a space

This should pull all paths which contain a filename with an extension, which looks like what you wanted from your first post. :mellow:

But I bet George can do better when he gets round to it! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

If, instead of a space, there was another character such as & (for example), there is a way to stop when it finds non-alphanumeric characters? Ie when no longer az, AZ, 0-9

Sorry for my excessive demands: u were fantastic :mellow:

Link to comment
Share on other sites

If, instead of a space, there was another character such as & (for example), there is a way to stop when it finds non-alphanumeric characters? Ie when no longer az, AZ, 0-9

Sorry for my excessive demands: u were fantastic :mellow:

Try this

$_String1 =  "1mtyutu78676"
ConsoleWrite ( "Is there non-alphanumeric characters : " & _IsNoAlphanumericCharInString ( $_String1 ) & @Crlf )

$_String2 =  "1mtyut!78676"
ConsoleWrite ( "Is there non-alphanumeric characters : " & _IsNoAlphanumericCharInString ( $_String2 ) & @Crlf )

Func _IsNoAlphanumericCharInString ( $_String )
    Return StringRegExp ( $_String, "(?i)[^a-z0-9]", 0 )
EndFunc ;==> _IsNoAlphanumericCharInString ( )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

You should apply here: (?U)[[:alpha:]]:\\.*\..*\x20

Let's say you should go instead of .*\x20

Because the extensions of the handles bad... must block any string other than an extension... based not only on the space...

Link to comment
Share on other sites

  • Moderators

Snoomi,

This should cover what you have asked for: :)

#include <Array.au3>

$sString = "blah blah blah blah C:\fred path\fred.ext blah blah blah blah D:\tom\dick\harry.dtdh2?blah blah blah blah E:\ blah blah blah blah"

$aRet = StringRegExp($sString, "(?U)[[:alpha:]]:\\.*\..*(?=[^[:alnum:]])" , 3)

_ArrayDisplay($aRet)

SRE:

(?U)             - Smallest match
[[:alpha:]]:\\   - Match a letter followed by :\
.*\.             - Followed by any number of characters until you find a "."
.*               - Followed by any number of characters
(?=[^[:alnum:]]) - Until a non-alphanumeric character, which will not be included in the match

Have we finally got there? :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Snoomi,

What did I say earlier about moving the goalposts? ;naughty:

I will look into it later if I find the time. :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

There are filenames that don't have an extension (like C:\Windows\System32\drivers\etc\hosts), so you can't rely on just regular expressions to do the job. A regular expression would either only look for files that have an extension, or also match folder names. To catch all files, you have to also collect directory names and filter them out later.

dim $aFiles[7] = [6, "Hi, C:\WINDOWS\explorer.exe", "C:\WINDOWS\system32\shell32.dll", "C:\", "C:\WINDOWS\", "Hi, run C:\WINDOWS\svchost.exe", "hardcore sample C:\Windows\System32\drivers\etc\hosts"]
;_arraydisplay($aFiles)

for $i = 1 to $aFiles[0]
    $path_and_file = stringregexp($aFiles[$i], "[a-z A-Z]\:\\.*", 1)
    if isarray($path_and_file) Then
        $filecheck = _isfile($path_and_file[0])
        if $filecheck = 1 then
            msgbox(0, "is a file", $path_and_file[0])
        Else
            msgbox(0, "is a directory", $path_and_file[0])
        EndIf
    EndIf
Next

func _isfile($string)
    $attrib = FileGetAttrib($string)
    If StringInStr($attrib, "D") Then return(0); return 0 if directory
    return(1); otherwise return 1
EndFunc
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...