Jump to content

help with: StringRegExpReplace()


scraps
 Share

Recommended Posts

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.

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

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

Link to comment
Share on other sites

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
Link to comment
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

Link to comment
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

Link to comment
Share on other sites

  • Moderators

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.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...