Sign in to follow this  
Followers 0
Baraoic

Reg Expression

5 posts in this topic

#1 ·  Posted (edited)

I have read the help file on regular expression and done lots of research. I have even tested with expresso, but AutoIt doesn't match the expression. I am trying to get the mac from the text

Physical Address    Transport Name
=================== ==========================================================
00-01-6C-EB-66-E9   \Device\Tcpip_{C5330785-8A4F-4B0D-9273-061C9F44D927}
Disabled            Disconnected
Disabled            Disconnected

using this code

MsgBox(0,'',_GetMAC())
Func _GetMAC()
    $Run = Run(@ComSpec & ' /c getmac', @SystemDir, @SW_HIDE, 6)
    While ProcessExists($Run)
        Sleep(100)
        $Read = StdoutRead($Run, True)
    WEnd
    $MAC = StringRegExp($Read,"(\w{2}\-){5}\w{2}",1)
    Return $MAC[0]
EndFunc

but it returns 66-

According to the help file and expresso that should work. I have tried using (((\w{2}\-){5})\w{2}) which did work but I shouldn't have to do that or do I?

Also while im on it I have an other one I use and cant get the non capturing group to work. The code I am using is

local $time2
$Run = Run(@ComSpec & ' /c w32tm /monitor', @SystemDir, @SW_HIDE, 6)
While ProcessExists($Run)
    Sleep(100)
    $Time = StdoutRead($Run, True)
WEnd
$aTime = StringSplit($Time, @LF)
For $x = 1 To UBound($aTime) - 1
    If StringRegExp($aTime[$x], "(NTP: )[+-][1-9]{1,9}\d{0,9}") Then; only if a server is off by a second or more
        $r = StringRegExp($aTime[$x], "(?i:NTP: )[+-][1-9]{1,9}\d{0,9}", 1)
        $s = StringRegExp($aTime[$x - 2], "\w{6,9}", 1); gets the server name of the server that is off time.
        $Time2 &= $s[0] & "   " & $r[0] & @LF
    EndIf
Next
MsgBox(0,'',$Time2)

Everything works fine on that its just the "ntp: " is suppose to be non capturing, but it isn't working correct. This isn't a big deal cause the script still works I just am curious as what I am doing wrong because I have gotten non capturing to work when its at the end of an expression just not beginning not sure if its designed like that or not.

Anyways thanks for the help.

Edited by Onichan

Share this post


Link to post
Share on other sites



A small example:

$MAC = "00-01-6C-EB-66-E9   \Device\Tcpip_{C5330785-8A4F-4B0D-9273-061C9F44D927}"
$res = StringRegExp($MAC, "^[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}", 1)

MsgBox(0, "", $res[0])

Share this post


Link to post
Share on other sites

Thanks for the reply which does work thanks, but I still dont understand why mine doesn't work according to the help file and reg expressions in general it should.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks for the reply which does work thanks, but I still dont understand why mine doesn't work according to the help file and reg expressions in general it should.

Your expression is a bit off... Your not telling it to capture anything after the fact, so it finds the entire string and goes from there.

So...

$r = StringRegExp($aTime[$x], "(?i:NTP: )[+-][1-9]{1,9}\d{0,9}", 1)

Might become

$r = StringRegExp($aTime[$x], "(?i)(?:NTP: )([+-][1-9]{1,9}\d{0,9})", 1)

Edit:

It also will work this way:

#include <array.au3>
Local $aTime[2] = ["NTP: -93933939993"]
$x = 0
$r = StringRegExp($aTime[$x], "(?i:NTP: )([+-][1-9]{1,9}\d{0,9})", 1)
_ArrayDisplay($r)
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

Thanks SmOke_N that worked perfectly. Didn't realize I had to do it that way, thanks.

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