Snoomi Posted August 9, 2011 Share Posted August 9, 2011 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.exeString: C:\WINDOWS\system32\shell32.dll -> Return: C:\WINDOWS\system32\shell32.dllString: C:\ -> NullString: C:\WINDOWS\ -> NullString: Hi, run C:\WINDOWS\svchost.exe -> Return: C:\WINDOWS\svchost.exeHow do I? Thanks for ur help and sorry for my English =( Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 9, 2011 Moderators Share Posted August 9, 2011 Snoomi, Welcome to the AutoIt forum. 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Snoomi Posted August 9, 2011 Author Share Posted August 9, 2011 I appreciate ur help... However, those were the only example... instead of "Hi,..." I could find any string containing a path. There is a universal way? Thanks =) Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted August 9, 2011 Share Posted August 9, 2011 exploder? .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 9, 2011 Moderators Share Posted August 9, 2011 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? 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Snoomi Posted August 9, 2011 Author Share Posted August 9, 2011 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 More sharing options...
Snoomi Posted August 9, 2011 Author Share Posted August 9, 2011 exploder? Which script is 3 hours. My brain melted.Yeah explorer >.< Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted August 9, 2011 Share Posted August 9, 2011 Perhaps it's _PathSplit() you're searching for? (helpfile > File Management) .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 9, 2011 Moderators Share Posted August 9, 2011 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. 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
GEOSoft Posted August 9, 2011 Share Posted August 9, 2011 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 More sharing options...
Moderators Melba23 Posted August 9, 2011 Moderators Share Posted August 9, 2011 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. But I bet George can do better when he gets round to it! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Snoomi Posted August 9, 2011 Author Share Posted August 9, 2011 Very thanks Melba23 and other. =) Link to comment Share on other sites More sharing options...
Snoomi Posted August 10, 2011 Author Share Posted August 10, 2011 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 Link to comment Share on other sites More sharing options...
wakillon Posted August 10, 2011 Share Posted August 10, 2011 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 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.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Snoomi Posted August 10, 2011 Author Share Posted August 10, 2011 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 More sharing options...
Moderators Melba23 Posted August 10, 2011 Moderators Share Posted August 10, 2011 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? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Snoomi Posted August 10, 2011 Author Share Posted August 10, 2011 But... String is C:\Windows\Microsoft.Net\Framework64\v3.0\WPF\PresentationFontCache.exe and return C:\Windows\Microsoft.Net !? Fix please xD Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 10, 2011 Moderators Share Posted August 10, 2011 Snoomi, What did I say earlier about moving the goalposts? ;naughty: I will look into it later if I find the time. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Snoomi Posted August 10, 2011 Author Share Posted August 10, 2011 Snoomi,What did I say earlier about moving the goalposts? ;naughty:I will look into it later if I find the time. M23OK, thanks and sorry Link to comment Share on other sites More sharing options...
Sven Posted August 11, 2011 Share Posted August 11, 2011 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now