Jump to content

RegEx and MAC Address


j0kky
 Share

Go to solution Solved by mikell,

Recommended Posts

Regular expressions, I hate them, seriously  :ranting:

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 by j0kky
Link to comment
Share on other sites

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 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.
Link to comment
Share on other sites

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 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.
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

×
×
  • Create New...