Jump to content

AHK RegEx to AutoIt Regex Conversion...


Recommended Posts

Hello,

 

A colleague of mine wrote an AHK script for parsing some data from clipboard, however he has left the company, and I have been tasked with converting his script into our main parsing tool.

 

Here is the RegEx he is using in his script:
 

Paste()
{
    thirdTab := InStr(Clipboard, "`t", False, 1, 3) + 1
    cleanPaste := RTrim(SubStr(Clipboard, thirdTab), "`t")
    If (InStr(cleanPaste, ".") And RegExMatch(cleanPaste, "i)(?=.*[A-Z])"))
    {
        CPArray := StrSplit(cleanPaste, ".")
        cleanPaste := CPArray.1
    }
    Clipboard := cleanPaste
    SendInput %Clipboard%
}

I am unable to get the expected output with all of the Regex I have tested...

Link to post
Share on other sites

Hm, an example of input and desired output data would be helpful.

What's your AutoIt Script looking like?

Anyway, some wild guessing:

Func paste()
    Local $tmp = ClipGet()
    $thirdTab = StringInStr($tmp, @TAB, 0, 3) + 1
    $cleanPaste = StringStripWS(StringMid($tmp, $thirdTab), 2)
    If StringInStr($cleanPaste, ".") And StringRegExp($cleanPaste, "(?i)(?=.*[A-Z])") Then
        Local $cparray = StringSplit($cleanPaste, ".")
        $cleanPaste = $cparray[1]
    EndIf
    ClipPut($cleanPaste)
    Send($cleanPaste)
EndFunc

 

Edited by Marc

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to post
Share on other sites

Sorry about that I completely forgot to include it:

 

Field   Value   
Summary Critical Alert: FAKEROUTERNAME_rl01.domain.com Role: Network Message: Device response time on FAKEROUTERNAME_rl01.domain.com triggered over warning threshold at 306 ms

 

Screen-Shot.png.0668b094d5e511794e1f11aa4bf6b49c.png

Please note any of those fields may be copied, and more below... The goal is to copy and only keep what is in the Value field... If it is just a device name remove all text after the last character before the first dot aka example above would be: fakeRoutername_rl01
I hope this makes sense... If any clarification is needed please let me know...

The code box above is a direct copy and paste of the first field...

 

 

Properties for event 14454186 on NCOMS.html

Edited by rm4453
Hid Proprietary Info
Link to post
Share on other sites

Hope I got that right...

paste()

Func paste()
    Local $tmp = ClipGet()
    $tmp = StringReplace($tmp, "Field" & @CRLF, "")
    $tmp = StringReplace($tmp, "Value" & @CRLF, "")
    $thirdTab = StringInStr($tmp, @TAB, 0, 1) + 1
    $cleanPaste = StringStripWS(StringMid($tmp, $thirdTab), 2)
    If StringInStr($cleanPaste, ".") And StringRegExp($cleanPaste, "(?i)(?=.*[A-Z])") Then
        Local $cparray = StringSplit($cleanPaste, ".")
        $cleanPaste = $cparray[1]
    EndIf
    ConsoleWrite($cleanPaste)
    ; ClipPut($cleanPaste)
    ; Send($cleanPaste)
EndFunc   ;==>paste

If I feed it the input string you posted, the result is "Critical Alert: FAKEROUTERNAME_rl01".

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to post
Share on other sites

Desired output from above paste should have been clarified further I am sorry about that here is desired output:

"FAKEROUTERNAME_rl01.domain.com Role: Network Message: Device response time on FAKEROUTERNAME_rl01.domain.com triggered over warning threshold at 306 ms"

Please note Most times only the following portion will be in that box:

Message: Device response time on FAKEROUTERNAME_rl01.domain.com triggered over warning threshold at 306 ms

 

In which cases the desired output would be:

"Device response time on FAKEROUTERNAME_rl01.domain.com triggered over warning threshold at 306 ms"

 

The desired output from the following copy & paste directly *box 2 of the page would be:

"FAKEROUTERNAME_rl01"

 

Field   Value   
Node    FAKEROUTERNAME_rl01.domain.com

 

 

Sorry It is middle of the night here and I just switched to grave-shift... 😕

Link to post
Share on other sites

Try this one. Worked at least in my tests and should be enough to get you started. 

I included some consolewrites for better tracking of what's done, so you can adjust it.

paste()

Func paste()
    Local $tmp = ClipGet()
    $tmp = StringReplace($tmp, "Field" & @CRLF & @TAB & @CRLF, "")
    $tmp = StringReplace($tmp, "Value" & @CRLF, "")
    ConsoleWrite("!" & $tmp & @CRLF)

    ; if a tab is present, get rid of the first value (description)
    if StringInStr($tmp, @TAB) Then
        $tmp = StringSplit($tmp, @TAB)
        $tmp = $tmp[2]
    EndIf
    ConsoleWrite("!" & $tmp & @CRLF)

    ; Remove the first part until ": "
    $tmp = StringRegExpReplace($tmp, ".*?: ", "", 1)
    ConsoleWrite("!" & $tmp & @CRLF)

    ; if there is no space in the result, its most likely just a computer name - so remove everything after the first .
    if not StringInStr($tmp, " ") Then $tmp = StringRegExpReplace($tmp, "\..*", "", 1)
    ConsoleWrite("!" & $tmp & @CRLF)

    ClipPut($cleanPaste)
    Send($cleanPaste)
EndFunc   ;==>paste

According to the original regex from your colleauge: either I'm too stupid to understand it or it's nonsene, because "(?=.*[A-Z])" means a forward reference. Which makes not much sense to me because there is no definition before.

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to post
Share on other sites

Maybe I completely misunderstood the requirements but couldn't something like this do the job ?

;$txt = "Field   Value   " & @crlf & _ 
;   "Summary Critical Alert: FAKEROUTERNAME_rl01.domain.com Role: Network Message: Device response time on FAKEROUTERNAME_rl01.domain.com triggered over warning threshold at 306 ms"
; Msgbox(0,"", $txt)
; Msgbox(0,"", StringRegExp($txt, '(\w+)\.', 1)[0] )

Msgbox(0,"", StringRegExp(ClipGet(), "\w\.") ? StringRegExp(ClipGet(), '(\w+)\.', 1)[0] : "error")

 

Edited by mikell
typo
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...