1

I'm not very good at regex with powershell and could use some help. The reason I'm using regex here is to accomidate for whitespaces (and any other text I can't think of atm that is not expected), so then the junk is stripped away to get the value that is wanted.

I'm trying the following code...

[String[]]$listOfStrings = @(' token = true ', ' token = false ', ' token = True ', ' token = False ')

For ($stringNumber = 0; $stringNumber -lt $listOfStrings.length; $stringNumber++)
{
    [String]$stringTest = ($listOfStrings[$stringNumber] -replace '*token*=','').Trim()
    Write-Host $stringTest
}

The output should be the following...

true
false
True
False

However, I'm getting the following error message...

Invalid regular expression pattern: *token*=.
At :line:5 char:63
+       [String]$stringTest = ($listOfStrings[$stringNumber] -replace <<<<  '*token*=','').Trim()

Any help is much appreciated.

1 Answer 1

2

Use:

[String]$stringTest = ($listOfStrings[$stringNumber] -replace '.*token.*=','').Trim()

Notice the .*.

You may also want to reference https://regex101.com/ in the future when you are writing your regular expressions. Many find that site quite useful.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately my work blocks that website, but I'll check it out and save as favs when I get home. Thanks a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.