Regex Exercises in R: 40 Practice Problems
Forty practice problems on regular expressions in R: anchors, character classes, quantifiers, groups, lookarounds. Solutions hidden.
Section 1. Anchors and basics (8 problems)
Exercise 1.1: Starts with
Difficulty: Beginner. Strings starting with "Mr".
Show solution
Exercise 1.2: Ends with
Difficulty: Beginner. ".csv" filenames.
Show solution
Exercise 1.3: Contains digit
Difficulty: Beginner.
Show solution
Exercise 1.4: Word boundary
Difficulty: Intermediate.
Show solution
Exercise 1.5: Whole-string match
Difficulty: Intermediate. Exactly 5 digits.
Show solution
Exercise 1.6: Empty string
Difficulty: Beginner.
Show solution
Exercise 1.7: Whitespace-only
Difficulty: Intermediate.
Show solution
Exercise 1.8: Case-insensitive
Difficulty: Intermediate.
Show solution
Section 2. Character classes and quantifiers (8 problems)
Exercise 2.1: Letters only
Difficulty: Beginner.
Show solution
Exercise 2.2: Digits only
Difficulty: Beginner.
Show solution
Exercise 2.3: Alphanumeric
Difficulty: Beginner.
Show solution
Exercise 2.4: Hex string
Difficulty: Intermediate.
Show solution
Exercise 2.5: Min length 8
Difficulty: Intermediate.
Show solution
Exercise 2.6: Exact length
Difficulty: Intermediate. 5 digits.
Show solution
Exercise 2.7: Range length
Difficulty: Intermediate. 3 to 6 letters.
Show solution
Exercise 2.8: Optional group
Difficulty: Advanced. Optional "+1" prefix on phones.
Show solution
Section 3. Groups and capturing (8 problems)
Exercise 3.1: Capture domain
Difficulty: Intermediate.
Show solution
Exercise 3.2: Multiple groups
Difficulty: Intermediate.
Show solution
Exercise 3.3: Named groups
Difficulty: Advanced.
Show solution
Exercise 3.4: Backreference in replacement
Difficulty: Advanced. Swap "John Smith" -> "Smith, John".
Show solution
Exercise 3.5: Non-capturing group
Difficulty: Advanced.
Show solution
Exercise 3.6: Alternation in group
Difficulty: Intermediate.
Show solution
Exercise 3.7: Repeated group
Difficulty: Advanced. Match repeated digits like "123-456-7890".
Show solution
Exercise 3.8: Match all groups
Difficulty: Advanced.
Show solution
Section 4. Lookarounds (6 problems)
Exercise 4.1: Lookbehind
Difficulty: Advanced. Digit after "$".
Show solution
Exercise 4.2: Negative lookbehind
Difficulty: Advanced.
Show solution
Exercise 4.3: Lookahead
Difficulty: Advanced. Digit BEFORE "px".
Show solution
Exercise 4.4: Negative lookahead
Difficulty: Advanced. Numbers NOT followed by "px".
Show solution
Exercise 4.5: Combined
Difficulty: Advanced.
Show solution
Exercise 4.6: Password rule
Difficulty: Advanced. At least one digit, one letter, length 8+.
Show solution
Section 5. Real-world (10 problems)
Exercise 5.1: Email
Difficulty: Intermediate.
Show solution
Exercise 5.2: URL
Difficulty: Intermediate.
Show solution
Exercise 5.3: ISO date
Difficulty: Intermediate.
Show solution
Exercise 5.4: Phone US
Difficulty: Intermediate.
Show solution
Exercise 5.5: Hashtag extraction
Difficulty: Intermediate.
Show solution
Exercise 5.6: Strip HTML tags
Difficulty: Advanced.
Show solution
Exercise 5.7: Mask credit card
Difficulty: Advanced.
Show solution
Exercise 5.8: Extract numbers from messy text
Difficulty: Intermediate.
Show solution
Exercise 5.9: First word
Difficulty: Intermediate.
Show solution
Exercise 5.10: Detect repeated words
Difficulty: Advanced. e.g. "the the".
Show solution
What to do next
- stringr-Exercises (shipped), string-tooling drills.
- Data-Cleaning-Exercises (shipped), regex inside cleanup.