Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Regex check for string between brackets

I'm trying to read values from a text file into a hashtable, I want to be able to be able to tell when I encounter a value that has the format "['somestring']"... So when I encounter a value that has brackets around it I want to store the string into a specific variable and run a function with that string.

I was thinking that regex was the way to go for this but I am unsure what a possible regex value would look like. Any help would be appreciated thank you!

like image 937
shreddish Avatar asked Sep 25 '26 07:09

shreddish


2 Answers

$r = [regex] "\[([^\[]*)\]"
$match = $r.match("[somestring]")
$text = $match.groups[1].value
like image 96
Lee Avatar answered Sep 27 '26 20:09

Lee


if("['somestring']" -match "\['([^\]]+)'\]")
{
    $matches[1]
}
like image 37
Shay Levy Avatar answered Sep 27 '26 20:09

Shay Levy