PowerShell - Regular ExpressionsHow can you use regex in PowerShell to replace all sequences of whitespace with a single space in a string?A$text -match '\s+', ' 'B$text -replace '\s+', ' 'C$text -like '\s+', ' 'D$text -contains '\s+', ' 'Check Answer
Step-by-Step SolutionSolution:Step 1: Identify the correct operator for replacementPowerShell uses '-replace' to substitute regex matches with new text.Step 2: Confirm regex pattern and replacement'\s+' matches one or more whitespace; replacing with single space achieves the goal.Final Answer:$text -replace '\s+', ' ' -> Option BQuick Check:Use -replace for regex substitutions [OK]Quick Trick: Use -replace with '\s+' to normalize whitespace [OK]Common Mistakes:Using -match or -like for replacementsConfusing -contains with regex operatorsNot using quantifier '+' for multiple spaces
Master "Regular Expressions" in PowerShell9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PowerShell Quizzes File and Directory Operations - Get-ChildItem for listing - Quiz 1easy File and Directory Operations - CSV operations (Import-Csv, Export-Csv) - Quiz 9hard Functions - Comment-based help - Quiz 10hard Functions - Why functions organize scripts - Quiz 5medium Functions - Comment-based help - Quiz 4medium Functions - Function definition - Quiz 15hard Regular Expressions - Regex with Select-String - Quiz 5medium Regular Expressions - Named captures - Quiz 10hard Regular Expressions - Regex quantifiers and anchors - Quiz 11easy Working with Objects - Object arrays - Quiz 4medium