简体   繁体   English

正则表达式-匹配字符然后捕获

[英]Regex - Matching characters then capturing

this is my first post on StackOverflow, and regex is new to me, please bear with me. 这是我在StackOverflow上的第一篇文章,正则表达式对我来说是新的,请耐心等待。

I am attempting to capture fields within a powershell command event log. 我正在尝试捕获powershell命令事件日志中的字段。 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" 我想捕获括号Get-ADUser中的字符串,也要捕获“用户名”的值字段

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”除外)。 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". 匹配括号内的任何内容,但“ 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: 更新-是否可以仅使用1个捕获组来捕获:

(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 [^x] :一个不是x 样本的字符
+ : One or more sample + :一个或多个样本
. : Any character except line break sample :除换行符示例外的任何字符
* : Zero or more times sample * :零次或多次采样
? : Once or none sample :一次或一次不取样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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