alien4u Posted April 20, 2016 Share Posted April 20, 2016 Thanks to @mikell I want to learn more about Regex I will use this example that someone ask few days ago: #include <String.au3> Global $sUrl = 'http://www.123anydomain.com/mytopfolder/various-possible-another/words/anotherpath_diff_/another/other/ok/Silk_By_Alien_By_Leonard' ConsoleWrite("Extracted first word: " & StringRegExpReplace($sUrl, "[\d\w\s\_\.\:\-\/]*/(.){1}|[\_\-\s][\d\w\s]*", "$1") & @CRLF) This is a working example but I would like to know if there is a better way to do it? short way to do it? Another example is to get destination drive letter base on the title of a File Copy Window(Super Copier4): Global $sUrl = "59% Copying from F:\folder\ to H:\folder\otropath" $sExtractDriveLetter = StringRegExpReplace($sUrl, "[\d\s\w].*(.):\\{1}.*", "$1") ConsoleWrite("Extracted Drive Letter: " & $sExtractDriveLetter & @CRLF) Regards Alien. Link to comment Share on other sites More sharing options...
mikell Posted April 20, 2016 Share Posted April 20, 2016 (edited) For these you can use the greedy feature of .* $sUrl = 'http://www.123anydomain.com/mytopfolder/various-possible-another/words/anotherpath_diff_/another/other/ok/Silk_By_Alien_By_Leonard' Msgbox(0,"", "Extracted first word: " & StringRegExpReplace($sUrl, ".*/([[:alpha:]]+).*", "$1") ) ".*/([[:alpha:]]+).*" the first .* : anything up to the last /[[:alpha:]]+ : one or more letters (avoid digits and underscore) $sUrl = "59% Copying from F:\folder\ to H:\folder\otropath" $sExtractDriveLetter = StringRegExpReplace($sUrl, ".*(\w):.*", "$1") Msgbox(0,"", "Extracted Drive Letter: " & $sExtractDriveLetter) ".*(\w):.*" the first .* : anything up to the last letter (\w) followed by a colon Edit Obviously there are plenty of other regex ways to skin these cats Edited April 20, 2016 by mikell Link to comment Share on other sites More sharing options...
alien4u Posted April 20, 2016 Author Share Posted April 20, 2016 1 hour ago, mikell said: For these you can use the greedy .* $sUrl = 'http://www.123anydomain.com/mytopfolder/various-possible-another/words/anotherpath_diff_/another/other/ok/Silk_By_Alien_By_Leonard' Msgbox(0,"", "Extracted first word: " & StringRegExpReplace($sUrl, ".*/([[:alpha:]]+).*", "$1") ) ".*/([[:alpha:]]+).*" the first .* : anything up to the last /[[:alpha:]]+ : one or more letters (avoid digits and underscore) $sUrl = "59% Copying from F:\folder\ to H:\folder\otropath" $sExtractDriveLetter = StringRegExpReplace($sUrl, ".*(\w):.*", "$1") Msgbox(0,"", "Extracted Drive Letter: " & $sExtractDriveLetter) ".*(\w):.*" the first .* : anything up to the last letter \w followed by a colon Thanks you so much @mikell I also saw another solution from you on the old post for the Example 1: ; Your Old Solution: StringRegExpReplace($sUrl, '.*/([^-]+).*', "$1") ; Mine base on yours: StringRegExpReplace($sUrl, '.*/([^\-\_\-\s]+).*', "$1") ; Last One from you that is the best one I think: StringRegExpReplace($sUrl, '.*/([[:alpha:]]+).*', "$1") So there is to many ways to do the same thing with Regex is all about how much you know it about Regex to use it and make it as simple and short as you can. Regards Alien. Link to comment Share on other sites More sharing options...
mikell Posted April 20, 2016 Share Posted April 20, 2016 The best solution is the one which 1) fits the requirements as much as possible, and 2) minimizes the risk of errors as much as possible Link to comment Share on other sites More sharing options...
alien4u Posted April 20, 2016 Author Share Posted April 20, 2016 What about repetition I mean you don't need last character or Word you need all the time X appear? Examples String: Extracted Drive Letter: Result: Parcel( 0x00000000: 00000000 0000000f 00350033 00330034 '........3.5.4.3.' 0x00000010: 00350033 00370030 00320030 00310036 '3.5.0.7.0.2.6.1.' 0x00000020: 00380030 00000038 '1.8.9... ') Result must be: 354335070261189 My working Regex is this one: StringRegExpReplace($info, "([\d])\.{1}|[\w\s\:\(\)\.\'\r\n]","$1") But I'm not able to make it shortest base on what I know and what I learn from you. Thanks Again. Regards Alien. Link to comment Share on other sites More sharing options...
mikell Posted April 20, 2016 Share Posted April 20, 2016 Please try this StringRegExpReplace($info, '\D|\d(?!\.)',"") means : "remove : non-digits, or digits which are not followed by a dot" Link to comment Share on other sites More sharing options...
alien4u Posted April 20, 2016 Author Share Posted April 20, 2016 1 hour ago, mikell said: Please try this StringRegExpReplace($info, '\D|\d(?!\.)',"") means : "remove : non-digits, or digits which are not followed by a dot" That works like a charm I did not think in negation I really need to Tune my logic with all this regex thing. Thanks you once again. Regards Alien. Link to comment Share on other sites More sharing options...
mikell Posted April 21, 2016 Share Posted April 21, 2016 But always keep in mind iamtheky's way which can save your day if you have trouble finding the right regex $s = "Extracted Drive Letter: Result: Parcel( " & @crlf & _ "0x00000000: 00000000 0000000f 00350033 00330034 '........3.5.4.3.' " & @crlf & _ "0x00000010: 00350033 00370030 00320030 00310036 '3.5.0.7.0.2.6.1.'" & @crlf & _ "0x00000020: 00380030 00000038 '1.8.9... ')" Local $res $s = StringStripWS(StringReplace($s, ".", ""), 8) StringReplace($s, "'", "'") For $i = 1 to @extended step 2 $res &= StringMid($s, StringInStr($s, "'", 0, $i) + 1, StringInStr($s, "'", 0, $i+1) - StringInStr($s, "'", 0, $i) - 1) Next Msgbox(0,"", $res) Link to comment Share on other sites More sharing options...
iamtheky Posted April 21, 2016 Share Posted April 21, 2016 skinning it again #include<array.au3> $s = "Extracted Drive Letter: Result: Parcel( " & @crlf & _ "0x00000000: 00000000 0000000f 00350033 00330034 '........3.5.4.3.' " & @crlf & _ "0x00000010: 00350033 00370030 00320030 00310036 '3.5.0.7.0.2.6.1.'" & @crlf & _ "0x00000020: 00380030 00000038 '1.8.9... ')" $aS = stringsplit($s , "." , 2) for $i = ubound($aS) - 1 to 0 step - 1 $aS[$i] = stringright(stringstripWS($aS[$i] , 8) , 1) If Binary($aS[$i]) < 0x30 OR Binary($aS[$i]) > 0x39 Then _ArrayDelete($aS , $i) Next msgbox(0, '' , _ArrayToString($aS , "")) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
iamtheky Posted April 21, 2016 Share Posted April 21, 2016 and one more, without array $sOut = "" $s = "Extracted Drive Letter: Result: Parcel( " & @crlf & _ "0x00000000: 00000000 0000000f 00350033 00330034 '........3.5.4.3.' " & @crlf & _ "0x00000010: 00350033 00370030 00320030 00310036 '3.5.0.7.0.2.6.1.'" & @crlf & _ "0x00000020: 00380030 00000038 '1.8.9... ')" $aS = stringsplit($s , "'" , 2) for $i = 0 to ubound($aS) - 1 If stringinstr($aS[$i] , ".") then $sOut &= stringreplace($aS[$i] , "." , "") Next msgbox(0, '' , $sOut) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
alien4u Posted April 21, 2016 Author Share Posted April 21, 2016 And I was doing this like this before the use of Regex: #include <Array.au3> #include <String.au3> $imei2 = "" $info = "Extracted Drive Letter: Result: Parcel( " & @crlf & _ "0x00000000: 00000000 0000000f 00350033 00330034 '........3.5.4.3.' " & @crlf & _ "0x00000010: 00350033 00370030 00320030 00310036 '3.5.0.7.0.2.6.1.'" & @crlf & _ "0x00000020: 00380030 00000038 '1.8.9... ')" $imei = _StringBetween($info," '","'") If IsArray($imei) Then For $i = 0 To UBound($imei) -1 $imei2 &= $imei[$i] Next $info = Int(StringReplace($imei2,".","")) EndIf ConsoleWrite($info&@CRLF) Regards Alien 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