Regular expressions (regex) are powerful tools for searching, matching, and cleaning text data. They are especially useful for psychology students—whether undergraduates, graduate students, or lab members—working with survey data, open-ended responses, or behavioral experiments.
"yes"
, "Yes"
, and "YES"
to the same value).wav
files that start with stim_
)You have 500 open-text responses to the question:
“How are you feeling today?”
You can use regex to find responses that mention:
tired
exhausted
fatigued
Even if they vary in casing (e.g., “Tired”, “exHAUSTED”) or are embedded in sentences.
Learning regular expressions makes you a more efficient and independent researcher, especially when working with:
A quick reference for building regular expressions — useful for coding, text analysis, and platforms like Canvas, Python, and R.
Pattern | Meaning |
---|---|
. |
Any character (except newline) |
\w |
Word character: [a-zA-Z0-9_] |
\W |
Non-word character: [^a-zA-Z0-9_] |
\d |
Digit: [0-9] |
\D |
Non-digit: [^0-9] |
\s |
Whitespace: space, tab, newline, etc. |
\S |
Non-whitespace |
Pattern | Meaning |
---|---|
* |
0 or more times |
+ |
1 or more times |
? |
0 or 1 time (optional) |
{n} |
Exactly n times |
{n,} |
n or more times |
{n,m} |
Between n and m times |
Pattern | Meaning |
---|---|
^ |
Start of string |
$ |
End of string |
\b |
Word boundary |
\B |
Not a word boundary |
Pattern | Meaning |
---|---|
(abc) |
Group exact pattern |
`a | b` |
[abc] |
Match any one character: a, b, or c |
[^abc] |
Match any character except a, b, or c |
Character | Escaped Form |
---|---|
. |
\. |
+ |
\+ |
* |
\* |
? |
\? |
` | ` |
( ) |
\( \) |
{ } |
\{ \} |
[ ] |
\[ \] |
\ |
\\ |
Flag | Meaning |
---|---|
(?i) |
Case-insensitive matching |
(?m) |
Multiline mode (^ /$ match line ends) |
(?s) |
Dot matches newline (. includes \n ) |
Pattern | Matches |
---|---|
`(?i)\btype\s+(i | 1)\b` |
\b\d{4}\b |
Any 4-digit number (e.g., “2024”) |
\s+ |
One or more whitespace characters |
`\b(yes | no |
^Hello |
“Hello” only at the start of the line |
✅ Pro Tip: Use
.*
to allow any number of characters between key words, and\s*
to allow optional spaces.