简体   繁体   中英

Regex - Matching characters then capturing

this is my first post on StackOverflow, and regex is new to me, please bear with me.

I am attempting to capture fields within a powershell command event log. I have text in the following format:

(Get-AdUser): name="Identity"; value="Username"

I want to capture the string inside the parenthesis Get-ADUser and also capture the value field of "username"

If possible a final output of

Get-AdUser Username

would be perfect.

The gotcha is that I want to capture any value inside the parenthesis except for the word "Out-Default". Out-Default is the output of a command, rather than the command itself.

So far I have:

\((?!Out-Default)([^)]+)\)

which is matching anything inside the parenthesis except "Out-Default".

I'm not sure how to approach it from here. Any advice is appreciated.

Update - is it possible to use only 1 capture group to capture:

(Get-AdUser): name="Identity"; value="Username"

and have the result look like

Get-AdUser name=Identity value=Username

?

Hope this work

\((?!Out-Default)([^)]+)\).*?value="([^"]+)"

Regex demo

Explanation:
\\ : Escapes a special character sample
( … ) : Capturing group sample
(?!…) : Negative lookahead sample
[^x] : One character that is not x sample
+ : One or more sample
. : Any character except line break sample
* : Zero or more times sample
? : Once or none sample

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM