Powershell using Regex finding a string inside a string

Need help with regex and powershell to accomplish the following. I have the following line of example:

<INPUT TYPE="hidden" NAME="site2pstoretoken" VALUE="v1.2~04C40A77~23"\><INPUT TYPE="hidden" NAME="p_error_code" VALUE="">

The only thing I want to extract from this line of the example is the hash stored in VALUE. The hash is very long, so I need to catch everything between VALUE = ".... HASH ...." \>

What will the regular expression look like?

+3
source share
1 answer

Try this with a warning that parsing html with regular expressions is a bad idea:

$regex = [regex]'(?<=VALUE=")[^"]*'
$regex.Match('te2pstoretoken" VALUE="v1.2~04C40A77~23"\><INP').Value

Edit: And this code also works:

if ('te2pstoretoken" VALUE="v1.2~04C40A77~23"\><INP' -match '(?<=VALUE=")[^"]*') { 
   $matches[0] 
}
+3
source

Source: https://habr.com/ru/post/1786174/


All Articles