Jump to content

need help with regex


Recommended Posts

 

#include <GuiToolBar.au3>

func _GetCPU()
    local static $instance = 1
    while TRUE
        local $handle = ControlGetHandle("[CLASS:Shell_TrayWnd]", "", "[CLASS:ToolbarWindow32;INSTANCE:" & $instance & "]")
        if $handle > 0 then
            for $i = 1 to _GUICtrlToolbar_ButtonCount($handle)
                local $text = _GUICtrlToolbar_GetButtonText($handle, _GUICtrlToolbar_IndexToCommand($handle, $i))
                if StringInStr($text, "CPU") then
                    ClipPut($text)
                    $text = StringRegExpReplace($text, "(CPU.*?C)(.+)", "$1")
                    return $text
                endif
            next
            $instance += 1
        else
            $instance = 1
            return ""
        endif
    wend
endfunc

while 1
    tooltip(_GETCPU())
    sleep(1000)
wend

 

 

$text is not an array and is assigned the multiline value 
CPU  51°C
GPU  45°C
C0%    9.6
MHz 2968
FID   29.75"

I want to extract the CPU temperature "CPU  51°C" from that multi-string.

 

Obviously $text = StringRegExpReplace($text, "(CPU.*?C)(.+)", "$1") isnt working.

I also tried "(.*)(CPU.*C)(.*)" with same result... 

I am only interested in "CPU ......C"

 

 

Link to comment
Share on other sites

1 hour ago, Marc said:

Try this way :)

Isn't there still a .* missing before (CPU.*?°C) ? Otherwise the lines before the "CPU line" are included.

Example :

Local $sText, $sTextNew
$sText = "some text before CPU line" & @CRLF & _
         "CPU  51°C" & @CRLF & _
         "GPU  45°C" & @CRLF & _
         "C0%    9.6" & @CRLF & _
         "MHz 2968" & @CRLF & _
         "FID   29.75"
ConsoleWrite("----> Original : ------------------------------------- " & @CRLF)
ConsoleWrite($sText & @CRLF)

ConsoleWrite("----> Marc : ----------------------------------------- " & @CRLF)
$sTextNew = StringRegExpReplace($sText,"(?s)(CPU.*?°C).*", "$1")
ConsoleWrite($sTextNew & @CRLF)

ConsoleWrite("----> Musashi : -------------------------------------- " & @CRLF)
$sTextNew = StringRegExpReplace($sText,"(?s).*(CPU.*?°C).*", "$1")
ConsoleWrite($sTextNew & @CRLF)

-> Output :

Spoiler

----> Original : -------------------------------------
some text before CPU line
CPU  51°C
GPU  45°C
C0%    9.6
MHz 2968
FID   29.75
----> Marc : -----------------------------------------
some text before CPU line
CPU  51°C
----> Musashi : --------------------------------------
CPU  51°C

Now we only have to wait for @mikell , who will provide a pattern which is only 5 characters long :D.

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

4 hours ago, Musashi said:

Isn't there still a .* missing before (CPU.*?°C) ?

I guess since y'all are using StringRegExpReplace, yes. However, if you use StringRegExp instead, then you don't have to worry about it.

Local $aRet = StringRegExp($sText, "(CPU\s+.*)", 3)
return $aRet[0]

; Or, if you're feeling daring, assume that you always want the first line...
Local $aRet = StringRegExp($sText, "(.*)", 3)
return $aRet[0]

As an added bonus, I got it in 4 characters, @Musashi :P

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

3 hours ago, seadoggie01 said:

I guess since y'all are using StringRegExpReplace, yes. However, if you use StringRegExp instead, then you don't have to worry about it.

You are right :). I just followed the questioner's specification, but StringRegExp is the more suitable function.

The pattern given by @mikell is the simplest (and obvious) solution when using StringRegExp here. It also matches, if any preceding line of text contains the word CPU.

Edited by Musashi
typo

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

7 hours ago, Musashi said:

Now we only have to wait for @mikell , who will provide a pattern which is only 5 characters long :D.

You summon the cat with your bell, yet you should realize that this ‘meal’ will hardly satisfy him, and he will now prowl for larger game :)

Code hard, but don’t hard code...

Link to comment
Share on other sites

  • 2 weeks later...

All the "Get CPU Temp scripts" (>10 years ago) found through search does not work and I have to run a third party Throttlestop or OpenHardwareMon or Core Temp to extract temperature from using this script...

why no get cpu temp directly that works anymore?

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