开发者

How does this Powershell Regex work?

开发者 https://www.devze.com 2023-04-13 01:56 出处:网络
\'<Relation From=\"701047080\" ObjectNumber=\"166543300\" Output=\"77\" To=\"464616324\">\' -match \'(?<=Outp开发者_运维问答ut=\")[^\"]*\'
'<Relation From="701047080" ObjectNumber="166543300" Output="77" To="464616324">' -match '(?<=Outp开发者_运维问答ut=")[^"]*'

$matchs then contains 77?

Which is what I want, I just don't understand why it works?

I've found stuff saying I could:

match '(?<NAME>Some regex)'

But I have no Idea what '(?<=' does? And I would REALLY like to understand the syntax...

I actually need to pull out the value of Output and To both... And only have powershell on the system I need to do this on...


The expression (?<=Output=") is a lookbehind. It matches only immediately after the string Output=".

The expression [^"]* is a negated character class. It matches all characters until the next double-quote (or to the end of the string if there are no more double-quotes).


The [^"]* says any number of characters other than ".

The [^"]* is the main expression.

The (?<=Output=") is a lookbehind and makes sure that the main expression is following the expression in the lookbehind, in this case Output=". The lookbehind expression is not included in the match

I don't think the regex is optimal.

You could have used:

'<Relation From="701047080" ObjectNumber="166543300" Output="77" To="464616324">' -match 'Output="(.*?)"'

(with the regex being as simple as Output="(.*?)")

and got the 77 in $matches[1]

The regex was probably just complicated with the lookbehind to make the Output=" be not part of the match. It makes the regex more complex and reduces performance. All you need is the above regex and extract 77 with the appropriate group.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号