Concept Flow - Common regex patterns
Start with input string
Apply regex pattern
Match found?
No→End: No match
Yes
Extract or use matched text
End
This flow shows how a regex pattern is applied to a string to find matches and extract text if found.
$text = 'Contact: user@example.com' $pattern = '\b\w+@\w+\.\w+\b' if ($text -match $pattern) { $matches[0] }
| Step | Action | Input | Regex Pattern | Match Result | Output |
|---|---|---|---|---|---|
| 1 | Set text variable | 'Contact: user@example.com' | N/A | N/A | |
| 2 | Set regex pattern | \b\w+@\w+\.\w+\b | N/A | N/A | |
| 3 | Apply -match operator | 'Contact: user@example.com' | \b\w+@\w+\.\w+\b | True | user@example.com |
| 4 | Extract matched text | user@example.com | user@example.com | ||
| 5 | End | N/A | N/A |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| $text | null | 'Contact: user@example.com' | 'Contact: user@example.com' | 'Contact: user@example.com' | 'Contact: user@example.com' |
| $pattern | null | null | \b\w+@\w+\.\w+\b | \b\w+@\w+\.\w+\b | \b\w+@\w+\.\w+\b |
| $matches[0] | null | null | null | user@example.com | user@example.com |
Common regex patterns in PowerShell:
- Use -match operator to test regex on strings
- Regex patterns need double backslashes in strings
- Matched text is stored in $matches[0]
- Example: '\b\w+@\w+\.\w+\b' matches emails
- Use if ($text -match $pattern) { $matches[0] } to extract