Bird
0
0

You want to extract all words starting with 'a' from $text = 'apple banana apricot cherry'. Which approach using -match works best?

hard📝 Application Q8 of 15
PowerShell - String Operations
You want to extract all words starting with 'a' from $text = 'apple banana apricot cherry'. Which approach using -match works best?
AUse <code>-match</code> with global flag to get all matches at once
BUse <code>$text -match 'a\w+'</code> once to get all matches
CUse a loop with <code>-match</code> on each word and collect matches
DUse <code>-match</code> with case-insensitive flag only
Step-by-Step Solution
Solution:
  1. Step 1: Understand -match behavior

    The -match operator returns True/False and only the first match in $Matches.
  2. Step 2: Extract multiple matches

    To get all words starting with 'a', loop over each word and apply -match individually.
  3. Final Answer:

    Use a loop with -match on each word and collect matches -> Option C
  4. Quick Check:

    -match finds first match; loop to find all [OK]
Quick Trick: Loop words to find multiple matches with -match [OK]
Common Mistakes:
  • Expecting -match to return all matches at once
  • Using global flag which -match does not support
  • Ignoring need to split text first

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes