extract a word after an IP address if given string is match

Anonymous
2021-02-04T18:40:12.817+00:00

trying to get Regex pattern to extract host name from an issue description line. tried with lot of pattern but unable to get it.

tried with pattern but dont know how to get server name.

CPU monitor on .*\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b\s-\s

issue text:
INFRA : Memory monitor on 172.16.0.1 - SERVER1 78% avg
INFRA : CPU monitor on 172.16.0.2 - SERVER2 100% used
INFRA : Disk space on 172.16.0.3 - SERVER3

condition: if "CPU monitor on" contains then extract SERVER2
expected output: SERVER2

Any help will be highly appreciated!!!

Microsoft System Center
Microsoft System Center
A suite of Microsoft systems management products that offer solutions for managing datacenter resources, private clouds, and client devices.
840 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,076 Reputation points
    2021-02-08T20:05:57.75+00:00

    I'm not real good with regex, but I enjoy a challenge. I got this Powershell code to work.

    $src = @("INFRA : Memory monitor on 172.16.0.1 - SERVER1 78% avg",
             "INFRA : CPU monitor on 172.16.0.2 - SERVER2 100% used",
             "INFRA : Disk space on 172.16.0.3 - SERVER3"
    )
    
    foreach ($line in $src) {
        if ($line -match '(?:(CPU monitor on).*(?:- ))(.*?(?=\s))') {
            "I found this line......: {0}" -f $line
            "And this server name...: {0}" -f $matches[2] 
            $matches
        }
    } 
    
    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AlexZhu-MSFT 5,551 Reputation points Microsoft Vendor
    2021-02-05T08:44:37.93+00:00

    Hi,

    Based on my understanding, regular expression is used to match a string. To extract the string, we may need other functions.

    Acoording to the expression above, it matches.

    64423-reg-exp-01.png

    If we want to extract a portion of a string, we may refer to the following article.
    https://stats.idre.ucla.edu/stata/faq/how-can-i-extract-a-portion-of-a-string-variable-using-regular-expressions/
    Note: this is not from MS, just for your reference.

    Hope the above information helps.

    Alex Zhu


    If the response is helpful, please click "Accept Answer" and upvote it.