stringr Exercises in R: 50 Real Practice Problems
Fifty practice problems on stringr in R: detect, extract, replace, split, count, pad, regex. Real scenarios with hidden solutions.
Section 1. Detect and match (8 problems)
Exercise 1.1: Detect substring
Difficulty: Beginner. Filter emails containing "gmail.com".
Show solution
Exercise 1.2: Detect at start
Difficulty: Beginner. Strings starting with "Mr ".
Show solution
Exercise 1.3: Detect at end
Difficulty: Beginner. Filenames ending with ".csv".
Show solution
Exercise 1.4: Count matches
Difficulty: Intermediate. Count vowels per word.
Show solution
Exercise 1.5: Position of match
Difficulty: Intermediate. Locate first digit position in each string.
Show solution
Exercise 1.6: Match case-insensitive
Difficulty: Intermediate. Detect "ERROR" anywhere, ignoring case.
Show solution
Exercise 1.7: Multi-word match
Difficulty: Intermediate. Filter rows where description contains BOTH "fast" AND "easy".
Show solution
Exercise 1.8: Filter rows of a tibble
Difficulty: Intermediate. From a tibble, keep rows whose name starts with "A".
Show solution
Section 2. Extract (8 problems)
Exercise 2.1: First match
Difficulty: Intermediate. Extract the first 3-digit number from each string.
Show solution
Exercise 2.2: All matches
Difficulty: Intermediate. Extract ALL numbers per string.
Show solution
Exercise 2.3: Capture groups
Difficulty: Advanced. From "user_42@x.com", capture "user" prefix and "42" id.
Show solution
Exercise 2.4: Extract email domain
Difficulty: Intermediate. Get the domain part of an email.
Show solution
Exercise 2.5: Extract phone area code
Difficulty: Intermediate. From "(415) 555-1234".
Show solution
Exercise 2.6: Extract dollars
Difficulty: Intermediate. From "Total: $123.45 paid".
Show solution
Exercise 2.7: Extract URL hostname
Difficulty: Advanced. From "https://r-statistics.co/path".
Show solution
Exercise 2.8: Multiple groups to columns
Difficulty: Advanced. From "John Smith, 30", extract first, last, age.
Show solution
Section 3. Replace and modify (8 problems)
Exercise 3.1: Replace first match
Difficulty: Beginner. Replace first digit with "*".
Show solution
Exercise 3.2: Replace all matches
Difficulty: Beginner. Replace all digits with "*".
Show solution
Exercise 3.3: Strip non-digits
Difficulty: Intermediate. Normalize phone to digits only.
Show solution
Exercise 3.4: Replace with backreference
Difficulty: Advanced. Reformat "John Smith" to "Smith, John".
Show solution
Exercise 3.5: Replace with named groups
Difficulty: Advanced. Same swap using named groups.
Show solution
Exercise 3.6: Trim whitespace
Difficulty: Beginner. Remove leading/trailing spaces.
Show solution
Exercise 3.7: Squish whitespace
Difficulty: Intermediate. Collapse internal multi-spaces too.
Show solution
Exercise 3.8: Remove punctuation
Difficulty: Intermediate. Strip all punctuation.
Show solution
Section 4. Split and combine (8 problems)
Exercise 4.1: Split by delimiter
Difficulty: Beginner. Split "a,b,c" on comma.
Show solution
Exercise 4.2: Split with simplify
Difficulty: Intermediate. Return a matrix.
Show solution
Exercise 4.3: Split into n parts
Difficulty: Intermediate. Split into max 2 pieces.
Show solution
Exercise 4.4: Concatenate vector
Difficulty: Beginner. Join words with " ".
Show solution
Exercise 4.5: Vectorized concatenate
Difficulty: Intermediate. Join two vectors element-wise.
Show solution
Exercise 4.6: Glue-style interpolation
Difficulty: Intermediate. Use stringr's str_glue for interpolation.
Show solution
Exercise 4.7: Pad to fixed width
Difficulty: Beginner. Zero-pad ID to 6 chars.
Show solution
Exercise 4.8: Truncate with ellipsis
Difficulty: Intermediate. Truncate to 10 chars with "...".
Show solution
Section 5. Case and length (8 problems)
Exercise 5.1: To lower
Difficulty: Beginner. Lowercase a string.
Show solution
Exercise 5.2: To upper
Difficulty: Beginner. Uppercase.
Show solution
Exercise 5.3: To title
Difficulty: Beginner. Title case.
Show solution
Exercise 5.4: To sentence
Difficulty: Intermediate. Capitalize first letter only.
Show solution
Exercise 5.5: String length
Difficulty: Beginner. Length of each.
Show solution
Exercise 5.6: Reverse a string
Difficulty: Intermediate. Reverse character order.
Show solution
Exercise 5.7: Substring by position
Difficulty: Intermediate. Get characters 2-4.
Show solution
Exercise 5.8: Replace substring by position
Difficulty: Advanced. Replace chars 2-4 with "XX".
Show solution
Section 6. Real workflows (10 problems)
Exercise 6.1: Validate email format
Difficulty: Intermediate. Detect strings that look like emails.
Show solution
Exercise 6.2: Extract hashtags
Difficulty: Intermediate. From a tweet, extract all #hashtags.
Show solution
Exercise 6.3: Parse a structured log line
Difficulty: Advanced. From "2024-01-15 ERROR [auth] timeout" extract date, level, module, msg.
Show solution
Exercise 6.4: Clean phone numbers
Difficulty: Intermediate. Normalize "(415) 555-1234" to "+14155551234".
Show solution
Exercise 6.5: Detect duplicates by normalized name
Difficulty: Intermediate. After lowercasing+trimming names, find duplicates.
Show solution
Exercise 6.6: Extract first sentence
Difficulty: Advanced. Pull the first sentence (up to ".", "!", or "?").
Show solution
Exercise 6.7: Strip HTML tags
Difficulty: Advanced. Remove <...> tags from a string.
Show solution
Exercise 6.8: Count words
Difficulty: Intermediate. Count words in a sentence.
Show solution
Exercise 6.9: Split sentences
Difficulty: Advanced. Split paragraph into sentences.
Show solution
Exercise 6.10: Mask sensitive info
Difficulty: Advanced. Replace credit card numbers (16 digits) with X.
Show solution
What to do next
- Regex-Exercises-in-R (coming), pure regex drilling.
- tidyverse-Exercises (shipped), string ops in pipelines.
- Data-Cleaning-Exercises (coming), strings as part of cleanup.