I need help with this regex. I get a string like below. I want something like
ServerName: AdminServer RUNNING
ServerName: ServicesServer1 RUNNING
$rv="xxx ServerName: AdminServer RUNNING xxxx ServerName: ServicesServer1 RUNNING xxx"
$rv -replace ".*(ServerName: [A-Za-z0-9]*.*[RUNNING|SHUTDOWN])",'$1'
ServerName: ServicesServer1 RUNNING xxx
With what I have now I only get the last match?
EDIT:
$rv="xxx ServerName: AdminServer RUNNING xxxx ServerName: ServicesServer1 RUNNING
$rv -replace ".*(ServerName: [A-Za-z0-9]*.*RUNNING|SHUTDOWN)",'$1'
gives
ServerName: ServicesServer1 RUNNING xxx
not
ServerName: AdminServer RUNNING
ServerName: ServicesServer1 RUNNING
EDIT2:
Turns out I have over simplified this. The string looks more like this:
$rv='XXX rtification Authority - G3,OU=(c) 2008 GeoTrust Inc. - For authorized use only,O=GeoTrust Inc.,C=US". The loading of the trusted certificate list raised a certificate parsing exception PKIX: Unsupported OID in the AlgorithmIdentifier object: 1.2.840.113549.1.1.11.>
Successfully Connected to Node Manager.
ServerName: SasTTpcAdminServer
RUNNING
Successfully disconnected from Node Manager.
Connecting to Node Manager ...
Successfully Connected to Node Manager.
ServerName: XXXServicesServer1
RUNNING
Successfully disconnected from Node Manager.'
While this is not necessarily an ideal approach, it will “work” after fixing some issues:
[RUNNING|SHUTDOWN]is a character calss, not an aliteration. Switch it to(?:RUNNING|SHUTDOWN)The
.*s matches too greedily. Use.*?instead.Final:
Notice how it doesn’t match the last bit .. however, there is likely a better solution to this (note that this uses the previously discussed fixes and some tweaks of my own):
Then we can map the capture group values into something simpler: