How to properly set a variable from PowerShell in Windows batch file?

后端 未结 3 405
夕颜
夕颜 2021-01-16 14:15

For someone unknown reasons, I am unable to retrieve this variable on Windows batch file.

PowerShell:

$datePattern = [Regex]::new(\'value=(\\S+)\')
$         


        
3条回答
  •  梦谈多话
    2021-01-16 14:55

    I'm not quite sure what exactly it is you want to get from the regex, but in your code you are re-defining the $datePattern immediately after setting it to value=(\S+).

    With \S+ you seem to not care about the format of the value, as long as it is something that is not whitespace. In the second definition of the regex, you DO want the value to be exactly two digits followed by a dot and then another digit.

    If that exact number format is what you seek, just do

    $datePattern = [Regex]::new('value=(\d\d\.\d)')
    

    When executed with

    $m = $datePattern.Matches("/ start=2010 / height=1 / value=12.2 / length=0.60 / users=264 / best=Adam /")
    

    you will have $m.Value which results in value=12.2
    and $m.Groups[1].Value which gives you the result of 12.2

    If you are looking for a numeric value but do not know the exact format, better change the regex to something like this: 'value=(\d+.\d+)'

提交回复
热议问题