Sign in to follow this  
Followers 0
scraps

help with: StringRegExpReplace()

8 posts in this topic

Hi Guys,

I have been trying to work with StringRegExpReplace() , but have had no such luck.

I am wanting to extract just the MACaddress from string.

77.10.119.232   - 255.255.254.0  - 00-15-46-5b-0t-z4   -27/09/2011 8:30:52 AM  -D-  PC0005.test.local

- after the IP there is 3 spaces then a hyphen, subnet, 2 white spaces, hypen, white space then mac address.

If someone could help me with a break down of various expressions I would use.

thanks in advance.

Share this post


Link to post
Share on other sites



That's not a valid MAC address because it has non-hex characters in it, but if it was valid:

$sString = "77.10.119.232   - 255.255.254.0  - 00-15-46-5b-07-e4   -27/09/2011 8:30:52 AM  -D-  PC0005.test.local"
ConsoleWrite("$sString = " & $sString & @LF)
$sString = StringRegExpReplace($sString, "[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}", "FE-DC-BA-98-76-54")
ConsoleWrite("$sString = " & $sString & @LF)

:graduated:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I'm not sure if this is the BEST way to do it, but I did it like so:

Global $String = "77.10.119.232   - 255.255.254.0  - 00-15-46-5b-0t-z4   -27/09/2011 8:30:52 AM  -D-  PC0005.test.local"
 
ConsoleWrite($String & @CRLF)
 
$NewString = StringRegExp($String,'(?i)(?s)255.(.*?)- (.*?) ',3)
 
ConsoleWrite($NewString[1])

And it read it just fine.

Edited by Damein

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi,

There's probably a better search pattern, but I gave it a shot.

Global $sString, $aSRE
 
$sString = "77.10.119.232   - 255.255.254.0  - 00-15-46-5b-0t-z4   -27/09/2011 8:30:52 AM  -D-  PC0005.test.local"
$aSRE = StringRegExp($sString, "(\w{2}-\w{2}-\w{2}-\w{2}-\w{2}-\w{2})", 1)
If IsArray($aSRE) Then
    MsgBox(64, "Success", "Found Mac Address: " & $aSRE[0])
Else
    MsgBox(64, "Failed", "No Mac Address found.")
EndIf

Edit: damn I'm so slow at posting..lol

Edited by smashly

Share this post


Link to post
Share on other sites

Hi everyone,

Thanks for your help,

Both Damien and Smashly's examples resulted in a win for me :graduated: ,

But so i can learn more about this function could i possibly get a break down?

StringRegExp($String,'(?i)(?s)255.(.*?)- (.*?) ',3)

- sets Case-insensitivity flag, matches anything including newline, (then im lost) think 255. is to do with the subnet mask.

tried to make sense of it in the help file but got little lost

Share this post


Link to post
Share on other sites

Hi,

Your better off using PsaltyDS version, since mac address is hex value.

In your mock up mac address string you have 0t-z4, a mac address will not have t or z.

mac address will only contain 0 to 9 and A to F.

Hence PsaltyDS using [:xdigit:] for a search pattern is the better method.

Global $sBogusMacAddress, $sMacAddress

$sBogusMacAddress = "77.10.119.232   - 255.255.254.0  - 00-15-46-5b-0t-z4   -27/09/2011 8:30:52 AM  -D-  PC0005.test.local"
MsgBox(64, "$sBogusMacAddress", _MacAddressFromString($sBogusMacAddress))

$sMacAddress = "77.10.119.232   - 255.255.254.0  - 00-15-46-5b-0F-F4   -27/09/2011 8:30:52 AM  -D-  PC0005.test.local"
MsgBox(64, "$sMacAddress", _MacAddressFromString($sMacAddress))

Func _MacAddressFromString($sString)
    Local $aSRE
    $aSRE = StringRegExp($sString, "[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}-[[:xdigit:]]{2}", 1)
    If IsArray($aSRE) Then Return "Found Mac Address: " & $aSRE[0]
    Return "No Mac Address found."
EndFunc

Cheers

Share this post


Link to post
Share on other sites

Thanks Smashly,

I do understand about Mac address being hex value, was just messing around with the values and guess i should have made the example in hex.

Thanks everyone for your help, most appreciated :graduated:

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

That pattern certainly looks like it could be shortened.... maybe:

$aSRE = StringRegExp($sString, "((?:[[:xdigit:]]{2}-){5}[[:xdigit:]]{2})", 1)
? Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
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
Sign in to follow this  
Followers 0