Bird
0
0

You want to extract all words starting with 'a' or 'A' from the string 'Apple and apricot are awesome'. Which PowerShell command correctly uses regex to do this?

hard📝 Application Q15 of 15
PowerShell - Regular Expressions
You want to extract all words starting with 'a' or 'A' from the string 'Apple and apricot are awesome'. Which PowerShell command correctly uses regex to do this?
A<code>[regex]::Matches('Apple and apricot are awesome', '\b[aA]\w*') | ForEach-Object { $_.Value }</code>
B<code>'Apple and apricot are awesome' -replace '\b[aA]\w*', ''</code>
C<code>'Apple and apricot are awesome' -match '\b[aA]\w*'</code>
D<code>[regex]::Matches('Apple and apricot are awesome', '\b[aA]\w*').Value</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal and regex

    The pattern '\b[aA]\w*' matches words starting with 'a' or 'A'.
  2. Step 2: Check PowerShell methods

    '-match' returns true/false, not matches. '-replace' removes text. [regex]::Matches returns all matches, but .Value returns only first match. Using ForEach-Object extracts all matched words.
  3. Final Answer:

    [regex]::Matches('Apple and apricot are awesome', '\b[aA]\w*') | ForEach-Object { $_.Value } -> Option A
  4. Quick Check:

    Use [regex]::Matches with ForEach-Object for all matches [OK]
Quick Trick: Use [regex]::Matches and loop to get all matches [OK]
Common Mistakes:
  • Using '-match' which returns only true/false
  • Using '-replace' which removes matched text
  • Using .Value on Matches returns only first match

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes