简体   繁体   中英

How does this Powershell Regex work?

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

$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.

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