j0kky Posted January 29, 2015 Posted January 29, 2015 (edited) Regular expressions, I hate them, seriously I have a MAC address in this format: "A2B3B56B7C3A" And I would like to change it in: "A2-B3-B5-6B-7C-3A" The MAC address can be a little longer than that one, but we are sure that the total digit number is an even number. I've tried a lot of patterns but I have not tried the right one. This is the closest one I've tried but the output shows a final hyphen (I could cut it through StringTrimLeft but I would like to do it using just RegEx). ConsoleWrite(StringRegExpReplace("A2B3B56B7C3A", "(.{2})", "$1-")) I don't understand why this pattern doesn't work ConsoleWrite(StringRegExpReplace("A2B3B56B7C3A", "^(?:.{2})(.{2})+?", "-$1")) Thanks in advance Edited January 29, 2015 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs
jdelaney Posted January 29, 2015 Posted January 29, 2015 (edited) Two steps, but could you will find someone widdle it down to 1: $string = "A2B3B56B7C3A" $a = StringRegExp($string,"(\w{2,2})",3) $mac = _ArrayToString($a,"-") ConsoleWrite($mac & @CRLF) I have no idea why _ArraytoString is byref...let's remove that: $string = "A2B3B56B7C3A" $mac = _ArrayToString2(StringRegExp($string,"(\w{2,2})",3),"-") ConsoleWrite($mac & @CRLF) Func _ArrayToString2($avArray, $sDelim = "|", $iStart = 0, $iEnd = 0) If Not IsArray($avArray) Then Return SetError(1, 0, "") If UBound($avArray, 0) <> 1 Then Return SetError(3, 0, "") Local $sResult, $iUBound = UBound($avArray) - 1 ; Bounds checking If $iEnd < 1 Or $iEnd > $iUBound Then $iEnd = $iUBound If $iStart < 0 Then $iStart = 0 If $iStart > $iEnd Then Return SetError(2, 0, "") ; Combine For $i = $iStart To $iEnd $sResult &= $avArray[$i] & $sDelim Next Return StringTrimRight($sResult, StringLen($sDelim)) EndFunc ;==>_ArrayToString Edited January 29, 2015 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
j0kky Posted January 29, 2015 Author Posted January 29, 2015 "(w{2,2})" pattern has the same matches of "(.{2})"... I would like to do it using just StringRegExpReplace... Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs
jdelaney Posted January 29, 2015 Posted January 29, 2015 (edited) The output for the string is the same, but the pattern is not. Although {2} and {2,2} is the same. Like I said, someone will get it down to one function. Not me. Edited January 29, 2015 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
mikell Posted January 29, 2015 Posted January 29, 2015 JJust say you don't want to add an hyphen at the end StringRegExpReplace("A2B3B56B7C3A", "(\w{2})(?!$)", "$1-")
spudw2k Posted January 29, 2015 Posted January 29, 2015 JJust say you don't want to add an hyphen at the end StringRegExpReplace("A2B3B56B7C3A", "(\w{2})(?!$)", "$1-") Good stuff. Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF
Solution mikell Posted January 29, 2015 Solution Posted January 29, 2015 (edited) You can also use this one based on an example (from Malkey ? not sure) to insert the hyphens in the righ places Msgbox(0,"", StringRegExpReplace("A2B3Bdd56B7C3A", "..\K(?!$)", "-") ) Edited January 29, 2015 by mikell
j0kky Posted January 29, 2015 Author Posted January 29, 2015 Cool examples Mikell! Thank you all! I don't like RegEx but sometimes it seems they're really useful Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs
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