pw <- c("Abcdef12", # ok
"12345678Zq", # ok, digits first
"abcdef12", # no uppercase
"MyPassword1", # contains password
"PASSWORD123x", # contains password, any case
"Pass1", # too short
paste0("A1", strrep("b", 20))) # 22 characters, too long
pw_expected <- c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)stringr: Lookarounds and Pipelines
Challenges, near-miss solutions, and the general rules
DATA 412/612, Week 9. Download the Quarto source and work the empty chunks in RStudio. This page runs the solutions.
1 How to use this document
Two parts, one workbook.
- Part I, Lookarounds. Checks that look at text without consuming it.
- Part II, From pattern to pipeline. The rest of stringr: matching, counting, extracting, splitting, replacing, cleaning, assembling, and sorting. Challenge 13 reuses the numbers from Challenge 3.
Each challenge has the same layout:
- The task, with test cases (the
expectedcolumn is the truth). - Your turn, an empty code chunk. Write your answer there and set
#| eval: falsetotrueto grade it. - Solution, collapsed. Try the challenge before opening it.
- Close, but wrong, collapsed. Realistic near-misses, each run against the same tests, with an explanation of why it breaks.
Part I ends with a general rule for building lookaround patterns. Part II ends with rules for choosing a function. Each part has its own pitfall checklist.
Conventions. In prose, patterns are written as the regex itself, for example \d+. In R code, backslashes are doubled, for example "\\d+". Everything here uses stringr, which runs on the ICU regex engine. Requires stringr 1.5.0 or later (for str_split_1(), str_flatten_comma(), str_escape()).
2 Part I. Lookarounds
2.1 Warm-up: the four tools
| Syntax | Name | Meaning |
|---|---|---|
(?=X) |
positive lookahead | X comes next |
(?!X) |
negative lookahead | X does not come next |
(?<=X) |
positive lookbehind | X comes right before |
(?<!X) |
negative lookbehind | X does not come right before |
Three facts drive every challenge in this part:
- Zero-width. A lookaround checks the text but does not consume it. After it succeeds, the engine is still at the same position.
- Lookbehind needs a bounded length.
(?<=a{1,3})is fine;(?<=a+)and(?<=a*)are errors in ICU. - Order matters only for consuming parts. Stacked lookaheads at the same position are an unordered AND. Consuming patterns chained with
.*are an ordered sequence.
2.2 Challenge 1: Password rules (unordered AND, plus a NOT)
Task. Write one pattern for str_detect() that requires all of:
- at least one digit and at least one uppercase letter, in any order
- the word “password” does not appear, in any capitalization
- length between 8 and 20 characters
Your turn
my_pattern <- "" # write your pattern here
check_detect(my_pattern, pw, pw_expected)sol1 <- "^(?=.{8,20}$)(?!.*(?i:password))(?=.*\\d)(?=.*[A-Z])"
check_detect(sol1, pw, pw_expected)| input | expected | got | verdict |
|---|---|---|---|
| Abcdef12 | TRUE | TRUE | pass |
| 12345678Zq | TRUE | TRUE | pass |
| abcdef12 | FALSE | FALSE | pass |
| MyPassword1 | FALSE | FALSE | pass |
| PASSWORD123x | FALSE | FALSE | pass |
| Pass1 | FALSE | FALSE | pass |
| A1bbbbbbbbbbbbbbbbbbbb | FALSE | FALSE | pass |
Reading it as a checklist, all from position 0:
(?=.{8,20}$): the whole string is 8 to 20 characters(?!.*(?i:password)): “password” appears nowhere, in any case(?=.*\d): a digit appears somewhere(?=.*[A-Z]): an uppercase letter appears somewhere
There is no consuming part, because str_detect() only needs the match to succeed.
Attempt A: the length lookahead has no $.
attempt1a <- "^(?=.{8,20})(?!.*(?i:password))(?=.*\\d)(?=.*[A-Z])"
check_detect(attempt1a, pw, pw_expected)| input | expected | got | verdict |
|---|---|---|---|
| Abcdef12 | TRUE | TRUE | pass |
| 12345678Zq | TRUE | TRUE | pass |
| abcdef12 | FALSE | FALSE | pass |
| MyPassword1 | FALSE | FALSE | pass |
| PASSWORD123x | FALSE | FALSE | pass |
| Pass1 | FALSE | FALSE | pass |
| A1bbbbbbbbbbbbbbbbbbbb | FALSE | TRUE | FAIL |
(?=.{8,20}) is satisfied as soon as the engine has seen 8 characters. It never checks that the string ends there, so a 22-character string passes. The upper bound only means something when $ follows it: (?=.{8,20}$).
Attempt B: a global (?i) flag.
attempt1b <- "(?i)^(?=.{8,20}$)(?!.*password)(?=.*\\d)(?=.*[A-Z])"
check_detect(attempt1b, pw, pw_expected)| input | expected | got | verdict |
|---|---|---|---|
| Abcdef12 | TRUE | TRUE | pass |
| 12345678Zq | TRUE | TRUE | pass |
| abcdef12 | FALSE | TRUE | FAIL |
| MyPassword1 | FALSE | FALSE | pass |
| PASSWORD123x | FALSE | FALSE | pass |
| Pass1 | FALSE | FALSE | pass |
| A1bbbbbbbbbbbbbbbbbbbb | FALSE | FALSE | pass |
Case-insensitivity makes “password” detection work, but the flag applies to the whole pattern, so [A-Z] now also matches lowercase letters and abcdef12 sneaks through. Scope the flag to the one part that needs it: (?i:password).
Attempt C: case-sensitive “password”.
attempt1c <- "^(?=.{8,20}$)(?!.*password)(?=.*\\d)(?=.*[A-Z])"
check_detect(attempt1c, pw, pw_expected)| input | expected | got | verdict |
|---|---|---|---|
| Abcdef12 | TRUE | TRUE | pass |
| 12345678Zq | TRUE | TRUE | pass |
| abcdef12 | FALSE | FALSE | pass |
| MyPassword1 | FALSE | TRUE | FAIL |
| PASSWORD123x | FALSE | TRUE | FAIL |
| Pass1 | FALSE | FALSE | pass |
| A1bbbbbbbbbbbbbbbbbbbb | FALSE | FALSE | pass |
password (lowercase) does not match Password or PASSWORD, so the forbidden word slips through whenever someone capitalizes it.
2.3 Challenge 2: Standalone numbers
Task. Use str_extract_all() to pull out numbers that are not attached to a letter or another digit on either side. For the string below, only 45 and 9 qualify.
txt2 <- "abc123 45 x6 78y 9"
expected2 <- "45, 9"Your turn
my_pattern <- "" # write your pattern here
check_extract_all(my_pattern, txt2, expected2)sol2 <- "(?<![A-Za-z\\d])\\d+(?![A-Za-z\\d])"
check_extract_all(sol2, txt2, expected2)| input | expected | got | verdict |
|---|---|---|---|
| abc123 45 x6 78y 9 | 45, 9 | 45, 9 | pass |
The lookbehind and lookahead both forbid a letter or a digit next to the number. Forbidding digits on both sides is what stops the engine from taking a piece of a longer number.
Attempt A: lookahead only, letters only.
check_extract_all("\\d+(?![A-Za-z])", txt2, expected2)| input | expected | got | verdict |
|---|---|---|---|
| abc123 45 x6 78y 9 | 45, 9 | 123, 45, 6, 7, 9 | FAIL |
Two bugs. With no lookbehind, 123 and 6 are extracted even though a letter sits in front of each. And for 78y, \d+ first tries 78, sees the y, backtracks to 7, and 7 is followed by 8, which is not a letter, so 7 passes.
Attempt B: lookbehind forbids letters only.
check_extract_all("(?<![A-Za-z])\\d+(?![A-Za-z])", txt2, expected2)| input | expected | got | verdict |
|---|---|---|---|
| abc123 45 x6 78y 9 | 45, 9 | 23, 45, 7, 9 | FAIL |
In abc123, the engine rejects starting at 1 (a letter is before it), then happily starts at 2, because a digit is not a letter. Hence 23. The same backtracking trick as above produces 7.
Attempt C: whitespace forbidden in the lookbehind.
check_extract_all("(?<![A-Za-z\\d\\s])\\d+(?![A-Za-z\\d])", txt2, expected2)| input | expected | got | verdict |
|---|---|---|---|
| abc123 45 x6 78y 9 | 45, 9 | (nothing) | FAIL |
Adding \s to the lookbehind forbids a space before the number, but 45 and 9 are both preceded by a space. The pattern can no longer match the very things we want.
Attempt D: word boundaries. This one is a near miss rather than an outright failure.
check_extract_all("\\b\\d+\\b", txt2, expected2)| input | expected | got | verdict |
|---|---|---|---|
| abc123 45 x6 78y 9 | 45, 9 | 45, 9 | pass |
It passes here, but \b treats the underscore as a word character, so it behaves differently from the explicit version when numbers touch underscores:
other <- "id_42 a-7 3.5"
str_extract_all(other, "(?<![A-Za-z\\d])\\d+(?![A-Za-z\\d])")[[1]] # explicit[1] "42" "7" "3" "5"
str_extract_all(other, "\\b\\d+\\b")[[1]] # word boundary[1] "7" "3" "5"
Whether 42 in id_42 should count is a question about the specification. The explicit version lets you decide; \b decides for you.
2.4 Challenge 3: Thousands separators that leave decimals alone
Task. Use str_replace_all() to insert commas into the integer part of a number, without touching digits after the decimal point. Challenge 13 solves the same task by consuming the decimal part instead of vetoing it.
nums <- c("1234567.891234", "12345.6789", "1000", "999", "0.123456", "123456.123456")
expected3 <- c("1,234,567.891234", "12,345.6789", "1,000", "999", "0.123456", "123,456.123456")Your turn
my_pattern <- "" # write your pattern here (the replacement is ",")
check_replace(my_pattern, nums, expected3, ",")sol3 <- "(?<=\\d)(?<!\\.\\d{0,20})(?=(\\d{3})+(?!\\d))"
check_replace(sol3, nums, expected3, ",")| input | expected | got | verdict |
|---|---|---|---|
| 1234567.891234 | 1,234,567.891234 | 1,234,567.891234 | pass |
| 12345.6789 | 12,345.6789 | 12,345.6789 | pass |
| 1000 | 1,000 | 1,000 | pass |
| 999 | 999 | 999 | pass |
| 0.123456 | 0.123456 | 0.123456 | pass |
| 123456.123456 | 123,456.123456 | 123,456.123456 | pass |
This pattern matches a position, not characters, so the replacement "," is inserted there. A position gets a comma when all three conditions hold:
(?<=\d): a digit is right before it (so no comma at the very start)(?=(\d{3})+(?!\d)): from here to the end of the digit run, the number of digits is an exact multiple of 3(?<!\.\d{0,20}): we are not inside a decimal part, meaning there is no.followed by up to 20 digits right before this position
The third condition is the trick. A lookbehind cannot be unbounded, so \d{0,20} is a bounded stand-in for “any run of digits”. It fails only for decimals longer than 20 digits.
Attempt A: the classic pattern, with no decimal protection.
check_replace("(?<=\\d)(?=(\\d{3})+(?!\\d))", nums, expected3, ",")| input | expected | got | verdict |
|---|---|---|---|
| 1234567.891234 | 1,234,567.891234 | 1,234,567.891,234 | FAIL |
| 12345.6789 | 12,345.6789 | 12,345.6,789 | FAIL |
| 1000 | 1,000 | 1,000 | pass |
| 999 | 999 | 999 | pass |
| 0.123456 | 0.123456 | 0.123,456 | FAIL |
| 123456.123456 | 123,456.123456 | 123,456.123,456 | FAIL |
This pattern is perfect for integers, but the decimal digits are also a run of digits, so 891234 becomes 891,234.
Attempt B: anchoring the lookahead with $.
check_replace("(?<=\\d)(?=(\\d{3})+$)", nums, expected3, ",")| input | expected | got | verdict |
|---|---|---|---|
| 1234567.891234 | 1,234,567.891234 | 1234567.891,234 | FAIL |
| 12345.6789 | 12,345.6789 | 12345.6,789 | FAIL |
| 1000 | 1,000 | 1,000 | pass |
| 999 | 999 | 999 | pass |
| 0.123456 | 0.123456 | 0.123,456 | FAIL |
| 123456.123456 | 123,456.123456 | 123456.123,456 | FAIL |
Now the integer part never gets commas when a decimal follows, since the lookahead needs the digits to run to the end of the string. The decimal part, on the other hand, still does.
Attempt C: unbounded lookbehind.
tryCatch(
str_replace_all(nums, "(?<=\\d)(?<!\\.\\d*)(?=(\\d{3})+(?!\\d))", ","),
error = function(e) cat(conditionMessage(e))
)Look-Behind pattern matches must have a bounded maximum length. (U_REGEX_LOOK_BEHIND_LIMIT, context=`(?<=\d)(?<!\.\d*)(?=(\d{3})+(?!\d))`)
The right idea (not preceded by a dot and some digits), but \d* has no upper bound, so ICU refuses to compile it.
In real code, format(x, big.mark = ",") or scales::comma() is usually the better tool. This challenge is about thinking in positions, which is a skill that transfers to harder problems.
2.5 Challenge 4: Repeated words (a backreference, not a lookaround)
Task. Write a str_detect() pattern that finds the same whole word twice in a row, ignoring case.
rw <- c("this is is a test", "The the cat", "no no no", "a a",
"this is a test", "island is", "this his", "is island")
expected4 <- c(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE)Your turn
my_pattern <- "" # write your pattern here
check_detect(my_pattern, rw, expected4)sol4 <- "(?i)\\b(\\w+)\\s+\\1\\b"
check_detect(sol4, rw, expected4)| input | expected | got | verdict |
|---|---|---|---|
| this is is a test | TRUE | TRUE | pass |
| The the cat | TRUE | TRUE | pass |
| no no no | TRUE | TRUE | pass |
| a a | TRUE | TRUE | pass |
| this is a test | FALSE | FALSE | pass |
| island is | FALSE | FALSE | pass |
| this his | FALSE | FALSE | pass |
| is island | FALSE | FALSE | pass |
(\w+) captures a word, \s+ is the gap, and \1 demands the same text again. The \b at both ends makes the match whole-word only. A pattern like this one is why regex is more than lookarounds: a lookaround can test “is something here”, but only a backreference can say “the same thing as before”.
Attempt A: no word boundaries.
check_detect("(\\w+)\\s+\\1", rw, expected4)| input | expected | got | verdict |
|---|---|---|---|
| this is is a test | TRUE | TRUE | pass |
| The the cat | TRUE | FALSE | FAIL |
| no no no | TRUE | TRUE | pass |
| a a | TRUE | TRUE | pass |
| this is a test | FALSE | TRUE | FAIL |
| island is | FALSE | FALSE | pass |
| this his | FALSE | TRUE | FAIL |
| is island | FALSE | TRUE | FAIL |
Without \b the engine can start in the middle of a word. In this is a test, \w+ can match the tail is of “this”, then a space, then is. Several wrong TRUEs follow from that, and The the is missed because the match is case-sensitive.
Attempt B: leading boundary only.
check_detect("\\b(\\w+)\\s+\\1", rw, expected4)| input | expected | got | verdict |
|---|---|---|---|
| this is is a test | TRUE | TRUE | pass |
| The the cat | TRUE | FALSE | FAIL |
| no no no | TRUE | TRUE | pass |
| a a | TRUE | TRUE | pass |
| this is a test | FALSE | FALSE | pass |
| island is | FALSE | FALSE | pass |
| this his | FALSE | FALSE | pass |
| is island | FALSE | TRUE | FAIL |
Starting at a word boundary fixes the first problem, but nothing stops the repeated word from being only the beginning of a longer one: in is island, the second is is a prefix of island. The trailing \b is what closes that gap.
Attempt C: case-sensitive.
check_detect("\\b(\\w+)\\s+\\1\\b", rw, expected4)| input | expected | got | verdict |
|---|---|---|---|
| this is is a test | TRUE | TRUE | pass |
| The the cat | TRUE | FALSE | FAIL |
| no no no | TRUE | TRUE | pass |
| a a | TRUE | TRUE | pass |
| this is a test | FALSE | FALSE | pass |
| island is | FALSE | FALSE | pass |
| this his | FALSE | FALSE | pass |
| is island | FALSE | FALSE | pass |
Structurally right, but The the fails. The fix is the (?i) flag; ICU applies it to the backreference as well.
2.6 Challenge 5: “ERROR” with no “IGNORE” before it
Task. Write a str_detect() pattern that is TRUE when the string contains ERROR and there is an ERROR with no IGNORE anywhere before it. An IGNORE after the ERROR is irrelevant.
lg <- c("ERROR disk full",
"ERROR then IGNORE",
"an ERROR then IGNORE ERROR", # first ERROR has no IGNORE before it
"IGNORE ERROR",
"IGNORE x ERROR ERROR",
"all good",
paste0("IGNORE ", strrep("x", 60), " ERROR")) # IGNORE far before ERROR
expected5 <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE)Your turn
my_pattern <- "" # write your pattern here
check_detect(my_pattern, lg, expected5)sol5 <- "^(?:(?!IGNORE).)*ERROR"
check_detect(sol5, lg, expected5)| input | expected | got | verdict |
|---|---|---|---|
| ERROR disk full | TRUE | TRUE | pass |
| ERROR then IGNORE | TRUE | TRUE | pass |
| an ERROR then IGNORE ERROR | TRUE | TRUE | pass |
| IGNORE ERROR | FALSE | FALSE | pass |
| IGNORE x ERROR ERROR | FALSE | FALSE | pass |
| all good | FALSE | FALSE | pass |
| IGNORE xxxxxxxxxxxxxxxxxxxxxxxxxx… | FALSE | FALSE | pass |
The key observation: some ERROR has no IGNORE before it if and only if the first ERROR has none. So we scan from the start, one character at a time, refusing to step onto the start of IGNORE, and stop successfully when ERROR appears. This construction, (?:(?!X).)*, is often called a tempered scan: it consumes anything except X.
Attempt A: the “negated chain” trap.
check_detect("^(?!.*IGNORE.*ERROR).*ERROR", lg, expected5)| input | expected | got | verdict |
|---|---|---|---|
| ERROR disk full | TRUE | TRUE | pass |
| ERROR then IGNORE | TRUE | TRUE | pass |
| an ERROR then IGNORE ERROR | TRUE | FALSE | FAIL |
| IGNORE ERROR | FALSE | FALSE | pass |
| IGNORE x ERROR ERROR | FALSE | FALSE | pass |
| all good | FALSE | FALSE | pass |
| IGNORE xxxxxxxxxxxxxxxxxxxxxxxxxx… | FALSE | FALSE | pass |
(?!.*IGNORE.*ERROR) means “there is no IGNORE followed by an ERROR anywhere”. That is much stronger than we need: in an ERROR then IGNORE ERROR the second ERROR has an IGNORE before it, which poisons the whole string, even though the first ERROR is perfectly clean.
Attempt B: forbidding IGNORE anywhere.
check_detect("^(?!.*IGNORE).*ERROR", lg, expected5)| input | expected | got | verdict |
|---|---|---|---|
| ERROR disk full | TRUE | TRUE | pass |
| ERROR then IGNORE | TRUE | FALSE | FAIL |
| an ERROR then IGNORE ERROR | TRUE | FALSE | FAIL |
| IGNORE ERROR | FALSE | FALSE | pass |
| IGNORE x ERROR ERROR | FALSE | FALSE | pass |
| all good | FALSE | FALSE | pass |
| IGNORE xxxxxxxxxxxxxxxxxxxxxxxxxx… | FALSE | FALSE | pass |
Even stronger, and wronger: any IGNORE anywhere kills the match, including ones that come after the ERROR.
Attempt C: bounded lookbehind.
check_detect("(?<!IGNORE.{0,50})ERROR", lg, expected5)| input | expected | got | verdict |
|---|---|---|---|
| ERROR disk full | TRUE | TRUE | pass |
| ERROR then IGNORE | TRUE | TRUE | pass |
| an ERROR then IGNORE ERROR | TRUE | TRUE | pass |
| IGNORE ERROR | FALSE | FALSE | pass |
| IGNORE x ERROR ERROR | FALSE | FALSE | pass |
| all good | FALSE | FALSE | pass |
| IGNORE xxxxxxxxxxxxxxxxxxxxxxxxxx… | FALSE | TRUE | FAIL |
This passes everything except the last case. A lookbehind must have a bounded length, so {0,50} can only see 50 characters back. When IGNORE is further away, the lookbehind misses it. The unbounded form (?<!IGNORE.*) would be exactly what we want, but ICU rejects it.
2.7 Challenge 6: Ordered chain plus a length check
Task. Write a str_detect() pattern that is TRUE when the string has a digit, then later an uppercase letter, then later a lowercase letter, and is at least 8 characters long.
oc <- c("1Aa56789", # digit, upper, lower, 8 chars
"ab1cdE2fg", # digit, upper, lower, 9 chars
"Abc12345", # right ingredients, wrong order
"abcdEFG1", # digit comes last, 8 chars
"9Xy", # right order, too short
"1A") # no lowercase after, too short
expected6 <- c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE)Your turn
my_pattern <- "" # write your pattern here
check_detect(my_pattern, oc, expected6)sol6 <- "^(?=.{8,}$).*\\d.*[A-Z].*[a-z]"
check_detect(sol6, oc, expected6)| input | expected | got | verdict |
|---|---|---|---|
| 1Aa56789 | TRUE | TRUE | pass |
| ab1cdE2fg | TRUE | TRUE | pass |
| Abc12345 | FALSE | FALSE | pass |
| abcdEFG1 | FALSE | FALSE | pass |
| 9Xy | FALSE | FALSE | pass |
| 1A | FALSE | FALSE | pass |
Length does not depend on order, so it gets a lookahead. The ordered requirement uses consuming .* between the three pieces, so each one must come after the previous one. No $ is needed at the end, since once the lowercase letter is found the rest of the string does not matter.
Attempt A: all lookaheads (unordered).
check_detect("^(?=.*\\d)(?=.*[A-Z])(?=.*[a-z]).{8,}$", oc, expected6)| input | expected | got | verdict |
|---|---|---|---|
| 1Aa56789 | TRUE | TRUE | pass |
| ab1cdE2fg | TRUE | TRUE | pass |
| Abc12345 | FALSE | TRUE | FAIL |
| abcdEFG1 | FALSE | TRUE | FAIL |
| 9Xy | FALSE | FALSE | pass |
| 1A | FALSE | FALSE | pass |
Every lookahead restarts from position 0, so this only checks that a digit, an uppercase and a lowercase exist somewhere. Abc12345 has all three, in the wrong order.
Attempt B: forgetting the leading .*.
check_detect("^(?=.{8,}$)\\d.*[A-Z].*[a-z]", oc, expected6)| input | expected | got | verdict |
|---|---|---|---|
| 1Aa56789 | TRUE | TRUE | pass |
| ab1cdE2fg | TRUE | FALSE | FAIL |
| Abc12345 | FALSE | FALSE | pass |
| abcdEFG1 | FALSE | FALSE | pass |
| 9Xy | FALSE | FALSE | pass |
| 1A | FALSE | FALSE | pass |
After ^(?=...), the engine is still at position 0, so \d must match the first character. ab1cdE2fg fails because it starts with a letter. The leading .* lets the digit appear anywhere.
2.8 Challenge 7: Splitting camelCase (inserting at positions)
Task. Use str_replace_all() with a single pattern and the replacement " " (a space) to split camelCase into words, including runs of capitals like HTML.
cc <- c("camelCaseString", "HTMLParser", "parseHTMLString", "simple")
expected7 <- c("camel Case String", "HTML Parser", "parse HTML String", "simple")Your turn
my_pattern <- "" # write your pattern here (the replacement is " ")
check_replace(my_pattern, cc, expected7, " ")sol7 <- "(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])"
check_replace(sol7, cc, expected7, " ")| input | expected | got | verdict |
|---|---|---|---|
| camelCaseString | camel Case String | camel Case String | pass |
| HTMLParser | HTML Parser | HTML Parser | pass |
| parseHTMLString | parse HTML String | parse HTML String | pass |
| simple | simple | simple | pass |
Both halves of the | are pure positions:
(?<=[a-z])(?=[A-Z]): between a lowercase letter and an uppercase letter (camel|Case)(?<=[A-Z])(?=[A-Z][a-z]): between two capitals when the second one starts a normal word (HTML|Parser: thePis followed bya)
Attempt A: only the lowercase-to-uppercase case.
check_replace("(?<=[a-z])(?=[A-Z])", cc, expected7, " ")| input | expected | got | verdict |
|---|---|---|---|
| camelCaseString | camel Case String | camel Case String | pass |
| HTMLParser | HTML Parser | HTMLParser | FAIL |
| parseHTMLString | parse HTML String | parse HTMLString | FAIL |
| simple | simple | simple | pass |
Fine for plain camelCase, but HTMLParser has no lowercase-to-uppercase transition at all, so nothing is inserted.
Attempt B: split before every capital.
check_replace("(?=[A-Z])", cc, expected7, " ")| input | expected | got | verdict |
|---|---|---|---|
| camelCaseString | camel Case String | camel Case String | pass |
| HTMLParser | HTML Parser | H T M L Parser | FAIL |
| parseHTMLString | parse HTML String | parse H T M L String | FAIL |
| simple | simple | simple | pass |
This shatters HTML into H T M L, because every capital is a valid split point. Position matching is powerful, so state the conditions as tightly as the problem needs.
2.9 Challenge 8: Username validation (rules from every direction)
Task. Write a str_detect() pattern for a valid username. All of these must hold:
- 3 to 12 characters long
- only ASCII letters, digits, and underscores
- starts with a letter
- does not end with an underscore
- no two underscores in a row
users <- c("john_doe", "User123", "abc", "abcdefghijkl", # valid (3 and 12 characters)
"_john", "9lives", # bad first character
"john_", # ends with underscore
"jo__hn", # double underscore
"jo", "abcdefghijklm", "a_b_c_d_e_f_g_h", # too short / too long
"bob-1", "user 1", "josé") # forbidden characters
users_expected <- c(TRUE, TRUE, TRUE, TRUE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)Your turn
my_pattern <- "" # write your pattern here
check_detect(my_pattern, users, users_expected)sol8 <- "^(?=.{3,12}$)(?!.*__)[A-Za-z][A-Za-z0-9_]*(?<!_)$"
check_detect(sol8, users, users_expected)| input | expected | got | verdict |
|---|---|---|---|
| john_doe | TRUE | TRUE | pass |
| User123 | TRUE | TRUE | pass |
| abc | TRUE | TRUE | pass |
| abcdefghijkl | TRUE | TRUE | pass |
| _john | FALSE | FALSE | pass |
| 9lives | FALSE | FALSE | pass |
| john_ | FALSE | FALSE | pass |
| jo__hn | FALSE | FALSE | pass |
| jo | FALSE | FALSE | pass |
| abcdefghijklm | FALSE | FALSE | pass |
| a_b_c_d_e_f_g_h | FALSE | FALSE | pass |
| bob-1 | FALSE | FALSE | pass |
| user 1 | FALSE | FALSE | pass |
| josé | FALSE | FALSE | pass |
The rules split naturally into two kinds:
- Whole-string conditions become lookaheads from the start: the length
(?=.{3,12}$)and the ban on__(?!.*__). - The shape of the string is the consuming part: one starting letter
[A-Za-z], then any allowed characters[A-Za-z0-9_]*.
The one rule that depends on the end of the string, “does not end with an underscore”, is a negative lookbehind placed just before $: (?<!_)$ means “the character just before the end is not an underscore”.
An equally valid version uses a negative lookahead instead:
check_detect("^(?=.{3,12}$)(?!.*__)(?!.*_$)[A-Za-z][A-Za-z0-9_]*$", users, users_expected)| input | expected | got | verdict |
|---|---|---|---|
| john_doe | TRUE | TRUE | pass |
| User123 | TRUE | TRUE | pass |
| abc | TRUE | TRUE | pass |
| abcdefghijkl | TRUE | TRUE | pass |
| _john | FALSE | FALSE | pass |
| 9lives | FALSE | FALSE | pass |
| john_ | FALSE | FALSE | pass |
| jo__hn | FALSE | FALSE | pass |
| jo | FALSE | FALSE | pass |
| abcdefghijklm | FALSE | FALSE | pass |
| a_b_c_d_e_f_g_h | FALSE | FALSE | pass |
| bob-1 | FALSE | FALSE | pass |
| user 1 | FALSE | FALSE | pass |
| josé | FALSE | FALSE | pass |
Attempt A: the length lookahead has no $.
check_detect("^(?=.{3,12})(?!.*__)[A-Za-z][A-Za-z0-9_]*(?<!_)$", users, users_expected)| input | expected | got | verdict |
|---|---|---|---|
| john_doe | TRUE | TRUE | pass |
| User123 | TRUE | TRUE | pass |
| abc | TRUE | TRUE | pass |
| abcdefghijkl | TRUE | TRUE | pass |
| _john | FALSE | FALSE | pass |
| 9lives | FALSE | FALSE | pass |
| john_ | FALSE | FALSE | pass |
| jo__hn | FALSE | FALSE | pass |
| jo | FALSE | FALSE | pass |
| abcdefghijklm | FALSE | TRUE | FAIL |
| a_b_c_d_e_f_g_h | FALSE | TRUE | FAIL |
| bob-1 | FALSE | FALSE | pass |
| user 1 | FALSE | FALSE | pass |
| josé | FALSE | FALSE | pass |
The familiar bug from Challenge 1. The closing $ at the end of the pattern anchors the consuming part, but the lookahead (?=.{3,12}) only checks that a 3-to-12 character prefix exists, so strings longer than 12 slip through.
Attempt B: \w instead of an explicit class.
check_detect("^(?=.{3,12}$)(?!.*__)[A-Za-z]\\w*(?<!_)$", users, users_expected)| input | expected | got | verdict |
|---|---|---|---|
| john_doe | TRUE | TRUE | pass |
| User123 | TRUE | TRUE | pass |
| abc | TRUE | TRUE | pass |
| abcdefghijkl | TRUE | TRUE | pass |
| _john | FALSE | FALSE | pass |
| 9lives | FALSE | FALSE | pass |
| john_ | FALSE | FALSE | pass |
| jo__hn | FALSE | FALSE | pass |
| jo | FALSE | FALSE | pass |
| abcdefghijklm | FALSE | FALSE | pass |
| a_b_c_d_e_f_g_h | FALSE | FALSE | pass |
| bob-1 | FALSE | FALSE | pass |
| user 1 | FALSE | FALSE | pass |
| josé | FALSE | TRUE | FAIL |
\w is not “letters, digits, underscore” in the ASCII sense: in ICU it also matches accented and non-Latin letters, so josé is accepted. When the rule says ASCII, spell the class out. (This demonstration needs a UTF-8 session; Quarto uses one by default, but an old R installation that reads the file in another encoding will not show the failure.)
Attempt C: forgetting the first-character rule.
check_detect("^(?=.{3,12}$)(?!.*__)[A-Za-z0-9_]+(?<!_)$", users, users_expected)| input | expected | got | verdict |
|---|---|---|---|
| john_doe | TRUE | TRUE | pass |
| User123 | TRUE | TRUE | pass |
| abc | TRUE | TRUE | pass |
| abcdefghijkl | TRUE | TRUE | pass |
| _john | FALSE | TRUE | FAIL |
| 9lives | FALSE | TRUE | FAIL |
| john_ | FALSE | FALSE | pass |
| jo__hn | FALSE | FALSE | pass |
| jo | FALSE | FALSE | pass |
| abcdefghijklm | FALSE | FALSE | pass |
| a_b_c_d_e_f_g_h | FALSE | FALSE | pass |
| bob-1 | FALSE | FALSE | pass |
| user 1 | FALSE | FALSE | pass |
| josé | FALSE | FALSE | pass |
With one class for every position, a leading underscore or digit is fine. The rule “starts with a letter” needs its own, separate character at the front.
Attempt D: forgetting the trailing-underscore rule.
check_detect("^(?=.{3,12}$)(?!.*__)[A-Za-z][A-Za-z0-9_]*$", users, users_expected)| input | expected | got | verdict |
|---|---|---|---|
| john_doe | TRUE | TRUE | pass |
| User123 | TRUE | TRUE | pass |
| abc | TRUE | TRUE | pass |
| abcdefghijkl | TRUE | TRUE | pass |
| _john | FALSE | FALSE | pass |
| 9lives | FALSE | FALSE | pass |
| john_ | FALSE | TRUE | FAIL |
| jo__hn | FALSE | FALSE | pass |
| jo | FALSE | FALSE | pass |
| abcdefghijklm | FALSE | FALSE | pass |
| a_b_c_d_e_f_g_h | FALSE | FALSE | pass |
| bob-1 | FALSE | FALSE | pass |
| user 1 | FALSE | FALSE | pass |
| josé | FALSE | FALSE | pass |
john_ passes because [A-Za-z0-9_]* allows an underscore in the last position. Rules about the end need either (?<!_)$ or (?!.*_$).
2.10 Challenge 9: The number after a label (bounded lookbehind)
Task. Extract every number that follows total:, allowing up to 3 spaces after the colon, without including the label in the result. A subtotal: does not count.
label_txt <- "total: 1234 and subtotal: 9 and total: 5 and total:42"
expected9 <- "1234, 5, 42"Your turn
my_pattern <- "" # write your pattern here
check_extract_all(my_pattern, label_txt, expected9)sol9 <- "(?<=\\btotal:\\s{0,3})\\d+"
check_extract_all(sol9, label_txt, expected9)| input | expected | got | verdict |
|---|---|---|---|
| total: 1234 and subtotal: 9 and total: 5 and total:42 | 1234, 5, 42 | 1234, 5, 42 | pass |
A lookbehind checks the label without including it in the result. Three details make it work:
\s{0,3}lets the spacing vary but is bounded, which ICU requires inside a lookbehind.\bbeforetotalstopssubtotal:from matching: there is no word boundary betweenbandtinsubtotal.\d+is the only consuming part, so only the number is returned.
A capture group gets the same answer when the label’s spacing is unbounded:
# column 2 of the match matrix holds the captured number
str_match_all(label_txt, "\\btotal:\\s*(\\d+)")[[1]][, 2][1] "1234" "5" "42"
Attempt A: no \b.
check_extract_all("(?<=total:\\s{0,3})\\d+", label_txt, expected9)| input | expected | got | verdict |
|---|---|---|---|
| total: 1234 and subtotal: 9 and total: 5 and total:42 | 1234, 5, 42 | 1234, 9, 5, 42 | FAIL |
subtotal: 9 contains the text total:, so its number is extracted too. A label with no boundary matches inside longer words.
Attempt B: a single fixed space.
check_extract_all("(?<=total: )\\d+", label_txt, expected9)| input | expected | got | verdict |
|---|---|---|---|
| total: 1234 and subtotal: 9 and total: 5 and total:42 | 1234, 5, 42 | 1234, 9 | FAIL |
Fixed text in a lookbehind is exact. total: 5 has three spaces and total:42 has none, so both are missed, while subtotal: 9 is wrongly included.
Attempt C: unbounded \s*.
tryCatch(
str_extract_all(label_txt, "(?<=\\btotal:\\s*)\\d+"),
error = function(e) cat(conditionMessage(e))
)Look-Behind pattern matches must have a bounded maximum length. (U_REGEX_LOOK_BEHIND_LIMIT, context=`(?<=\btotal:\s*)\d+`)
The natural way to write it, and ICU rejects it: the lookbehind must have a bounded maximum length. \s{0,3} is the workaround, at the cost of failing on 4 or more spaces.
Attempt D: no lookbehind.
check_extract_all("\\btotal:\\s*\\d+", label_txt, expected9)| input | expected | got | verdict |
|---|---|---|---|
| total: 1234 and subtotal: 9 and total: 5 and total:42 | 1234, 5, 42 | total: 1234, total: 5, total:42 | FAIL |
This finds the right places, but the label is part of the match, so the result contains total: 1234 instead of 1234. Lookarounds exist precisely so that the context can be required without being returned.
2.11 Challenge 10: Splitting on commas outside parentheses
Task. Use str_split_1() to split a string on commas, but not on commas inside parentheses. Parentheses are not nested.
cs <- c("a,b,c",
"a,f(b,c),d",
"f(1,2),g(3,4)",
"x(1,2),(3,4),y",
"f(a,b,c)",
"no commas")
cs_expected <- list(c("a", "b", "c"),
c("a", "f(b,c)", "d"),
c("f(1,2)", "g(3,4)"),
c("x(1,2)", "(3,4)", "y"),
"f(a,b,c)",
"no commas")Your turn
my_pattern <- "" # write the pattern for the separator
check_split(my_pattern, cs, cs_expected)sol10 <- ",(?![^(]*\\))"
check_split(sol10, cs, cs_expected)| input | expected | got | verdict |
|---|---|---|---|
| a,b,c | a ¦ b ¦ c | a ¦ b ¦ c | pass |
| a,f(b,c),d | a ¦ f(b,c) ¦ d | a ¦ f(b,c) ¦ d | pass |
| f(1,2),g(3,4) | f(1,2) ¦ g(3,4) | f(1,2) ¦ g(3,4) | pass |
| x(1,2),(3,4),y | x(1,2) ¦ (3,4) ¦ y | x(1,2) ¦ (3,4) ¦ y | pass |
| f(a,b,c) | f(a,b,c) | f(a,b,c) | pass |
| no commas | no commas | no commas | pass |
The pattern matches a comma that is not followed by [^(]*\). Read the lookahead as: “from here, skip any characters that are not (, then see a )”. If that is true, we are inside parentheses: a ) shows up before any new ( opens. A comma outside parentheses is followed either by a ( first, or by no ) at all, so the lookahead fails and the comma is a real separator.
The key is [^(]* rather than .*. It limits how far ahead the lookahead is allowed to look.
Attempt A: a plain comma.
check_split(",", cs, cs_expected)| input | expected | got | verdict |
|---|---|---|---|
| a,b,c | a ¦ b ¦ c | a ¦ b ¦ c | pass |
| a,f(b,c),d | a ¦ f(b,c) ¦ d | a ¦ f(b ¦ c) ¦ d | FAIL |
| f(1,2),g(3,4) | f(1,2) ¦ g(3,4) | f(1 ¦ 2) ¦ g(3 ¦ 4) | FAIL |
| x(1,2),(3,4),y | x(1,2) ¦ (3,4) ¦ y | x(1 ¦ 2) ¦ (3 ¦ 4) ¦ y | FAIL |
| f(a,b,c) | f(a,b,c) | f(a ¦ b ¦ c) | FAIL |
| no commas | no commas | no commas | pass |
Every comma is a separator, including those inside parentheses.
Attempt B: .* inside the lookahead.
check_split(",(?!.*\\))", cs, cs_expected)| input | expected | got | verdict |
|---|---|---|---|
| a,b,c | a ¦ b ¦ c | a ¦ b ¦ c | pass |
| a,f(b,c),d | a ¦ f(b,c) ¦ d | a,f(b,c) ¦ d | FAIL |
| f(1,2),g(3,4) | f(1,2) ¦ g(3,4) | f(1,2),g(3,4) | FAIL |
| x(1,2),(3,4),y | x(1,2) ¦ (3,4) ¦ y | x(1,2),(3,4) ¦ y | FAIL |
| f(a,b,c) | f(a,b,c) | f(a,b,c) | pass |
| no commas | no commas | no commas | pass |
(?!.*\)) means “no ) anywhere later in the string”. Any closing parenthesis later in the text, even one belonging to a different group, makes every earlier comma look like it is inside parentheses. Scoping matters: [^(]* stops at the next (, .* never stops.
Attempt C: the reversed scope.
check_split(",(?![^)]*\\()", cs, cs_expected)| input | expected | got | verdict |
|---|---|---|---|
| a,b,c | a ¦ b ¦ c | a ¦ b ¦ c | pass |
| a,f(b,c),d | a ¦ f(b,c) ¦ d | a,f(b ¦ c) ¦ d | FAIL |
| f(1,2),g(3,4) | f(1,2) ¦ g(3,4) | f(1 ¦ 2),g(3 ¦ 4) | FAIL |
| x(1,2),(3,4),y | x(1,2) ¦ (3,4) ¦ y | x(1 ¦ 2),(3 ¦ 4) ¦ y | FAIL |
| f(a,b,c) | f(a,b,c) | f(a ¦ b ¦ c) | FAIL |
| no commas | no commas | no commas | pass |
This asks “is a ( coming before the next )?”, which is true for a comma before a group, not inside one. It protects commas that should be split and splits those that should be protected.
A known limit. This pattern does not handle nested parentheses. Regular expressions cannot count matching pairs:
str_split_1("f(a,g(b)),c", sol10) # should be "f(a,g(b))" and "c"[1] "f(a" "g(b))" "c"
The comma after a is followed by g(, so the lookahead sees a ( first and decides the comma is outside. For arbitrarily nested input, use a real parser.
2.12 Avoiding an unwanted region
Several challenges above share one idea: some matches are fine and others must be avoided because of where they sit. The decimal digits in Challenge 3, the commas inside parentheses in Challenge 10, and the ERROR that follows an IGNORE in Challenge 5 are all examples. This part names that idea, lists a technique for each kind of region, and then practises the two techniques the earlier challenges did not use.
2.12.1 The idea
Think of every position in the string as an address. Some addresses are off limits: “after the decimal point”, “inside the parentheses”, “after the word IGNORE”. Avoiding an address means checking, at each candidate spot, whether it is in that region, and refusing the match if it is.
The recipe has four steps:
- Name the unwanted region precisely: “digits after a dot”, “anything inside quotes”.
- Find its landmark: the marker that tells you that you are inside, such as
.,(,", or the wordIGNORE. - Locate the landmark relative to the candidate spot: right next to it, somewhere before it, or somewhere after it?
- Look in that direction and negate. Write “the landmark is there”, then put
!on it:(?<!...)to look back,(?!...)to look ahead.
When the region has no tidy landmark, flip the approach: match the unwanted region itself first, so the engine swallows it and moves on.
| The unwanted region is… | Technique | Template | Example |
|---|---|---|---|
| right next to the spot | plain negative lookaround | (?<!A)X(?!B) |
(?<!black )cat(?!s) skips “black cat” and “cats” |
| after a marker, within N characters | bounded lookbehind | (?<!MARKER.{0,N}) |
\d(?<!\.\d{0,20}) skips decimal digits (Challenge 3) |
| after a marker, any distance | tempered scan from ^ |
^(?:(?!MARKER).)*X |
^(?:(?!IGNORE).)*ERROR (Challenge 5) |
| inside brackets | scoped lookahead | X(?![^(]*\)) |
,(?![^(]*\)) (Challenge 10) |
| inside quotes | parity lookahead | X(?=(?:[^"]*"[^"]*")*[^"]*$) |
Challenge 11 |
| anything easy to match as a whole | consume it first | REGION\|X with a function |
Challenges 12 and 13 |
The first four rows are vetoes: the match is attempted everywhere and refused in the wrong places. The last row is consumption: the unwanted region is matched on purpose, so it can never be mistaken for a wanted match.
2.13 Challenge 11: Commas outside quotes (a parity check)
Task. Use str_split_1() to split on commas that are not inside double quotes. Quotes are balanced and are never escaped.
qs <- c('a,"b,c",d',
'x,y',
'"p,q"',
'a,"b,c","d,e,f",g',
'one')
qs_expected <- list(c('a', '"b,c"', 'd'),
c('x', 'y'),
'"p,q"',
c('a', '"b,c"', '"d,e,f"', 'g'),
'one')Your turn
my_pattern <- "" # write the pattern for the separator
check_split(my_pattern, qs, qs_expected)sol11 <- ',(?=(?:[^"]*"[^"]*")*[^"]*$)'
check_split(sol11, qs, qs_expected)| input | expected | got | verdict |
|---|---|---|---|
| a,“b,c”,d | a ¦ “b,c” ¦ d | a ¦ “b,c” ¦ d | pass |
| x,y | x ¦ y | x ¦ y | pass |
| “p,q” | “p,q” | “p,q” | pass |
| a,“b,c”,“d,e,f”,g | a ¦ “b,c” ¦ “d,e,f” ¦ g | a ¦ “b,c” ¦ “d,e,f” ¦ g | pass |
| one | one | one | pass |
There is no closing landmark to look for, so use counting. A comma is outside quotes exactly when an even number of quotes remains to its right. The lookahead says so:
(?:[^"]*"[^"]*")*is “zero or more complete pairs of quotes”, with any non-quote text around them.[^"]*$is “then only non-quote text until the end of the string”.
Inside a quoted section, an odd number of quotes remains. The pairs cannot use them all up, so the lookahead cannot reach $ and fails, and that comma is not a separator.
Attempt A: a plain comma.
check_split(",", qs, qs_expected)| input | expected | got | verdict |
|---|---|---|---|
| a,“b,c”,d | a ¦ “b,c” ¦ d | a ¦ “b ¦ c” ¦ d | FAIL |
| x,y | x ¦ y | x ¦ y | pass |
| “p,q” | “p,q” | “p ¦ q” | FAIL |
| a,“b,c”,“d,e,f”,g | a ¦ “b,c” ¦ “d,e,f” ¦ g | a ¦ “b ¦ c” ¦ “d ¦ e ¦ f” ¦ g | FAIL |
| one | one | one | pass |
Attempt B: “no quote ahead”.
check_split(',(?=[^"]*$)', qs, qs_expected)| input | expected | got | verdict |
|---|---|---|---|
| a,“b,c”,d | a ¦ “b,c” ¦ d | a,“b,c” ¦ d | FAIL |
| x,y | x ¦ y | x ¦ y | pass |
| “p,q” | “p,q” | “p,q” | pass |
| a,“b,c”,“d,e,f”,g | a ¦ “b,c” ¦ “d,e,f” ¦ g | a,“b,c”,“d,e,f” ¦ g | FAIL |
| one | one | one | pass |
This only allows a split when no quote at all follows. That is true after the last quote, so everything before it stays glued together.
Attempt C: parity without the $.
check_split(',(?=(?:[^"]*"[^"]*")*[^"]*)', qs, qs_expected)| input | expected | got | verdict |
|---|---|---|---|
| a,“b,c”,d | a ¦ “b,c” ¦ d | a ¦ “b ¦ c” ¦ d | FAIL |
| x,y | x ¦ y | x ¦ y | pass |
| “p,q” | “p,q” | “p ¦ q” | FAIL |
| a,“b,c”,“d,e,f”,g | a ¦ “b,c” ¦ “d,e,f” ¦ g | a ¦ “b ¦ c” ¦ “d ¦ e ¦ f” ¦ g | FAIL |
| one | one | one | pass |
It behaves exactly like a plain comma. Without $, the lookahead can succeed by matching zero pairs and then stopping anywhere, so it never actually checks the count. Counting only works when the check has to reach the end of the string.
A known limit. The parity trick assumes balanced quotes with no escaped quotes (\") inside. For real CSV files, use a CSV reader instead.
2.14 Challenge 12: Replace outside quotes (consume first)
Task. Replace the whole word cat with dog, except inside double quotes. Use str_replace_all(); the replacement may be a function.
How a function replacement works in stringr. str_replace_all() does not call your function once per match. It calls it once, passing a character vector of all the matches, and it expects a character vector of the same length back. So the function must be vectorized:
- use
ifelse()(ordplyr::if_else()), never a scalarif (...) ... else ... - never return a single fixed value such as
"dog"; userep("dog", length(m))instead
Breaking this rule aborts with: replacement function must return a vector the same length as the input.
ss <- c('cat "cat" cat',
'a cat sat',
'"cat" only',
'concatenate cat',
'"a cat here" cat "another cat"')
ss_expected <- c('dog "cat" dog',
'a dog sat',
'"cat" only',
'concatenate dog',
'"a cat here" dog "another cat"')Your turn
my_pattern <- ""
my_replacement <- function(m) m # write the replacement logic here (vectorized!)
check_replace(my_pattern, ss, ss_expected, my_replacement)keep_or_dog <- function(m) ifelse(str_starts(m, '"'), m, "dog")
check_replace('"[^"]*"|\\bcat\\b', ss, ss_expected, keep_or_dog)| input | expected | got | verdict |
|---|---|---|---|
| cat “cat” cat | dog “cat” dog | dog “cat” dog | pass |
| a cat sat | a dog sat | a dog sat | pass |
| “cat” only | “cat” only | “cat” only | pass |
| concatenate cat | concatenate dog | concatenate dog | pass |
| “a cat here” cat “another cat” | “a cat here” dog “another cat” | “a cat here” dog “another cat” | pass |
The pattern has two branches:
"[^"]*"matches a whole quoted section. The engine consumes it, so anycatinside it is never seen as a separate match.\bcat\bmatches the word outside quotes.
The function then looks at which branch matched: a match starting with " is returned unchanged, and anything else becomes dog. Because ifelse() works on the whole vector of matches at once, it returns one answer per match, which is exactly what stringr requires. The same answer can also be reached with a veto, using the parity lookahead from Challenge 11, which needs no function at all:
check_replace('\\bcat\\b(?=(?:[^"]*"[^"]*")*[^"]*$)', ss, ss_expected, "dog")| input | expected | got | verdict |
|---|---|---|---|
| cat “cat” cat | dog “cat” dog | dog “cat” dog | pass |
| a cat sat | a dog sat | a dog sat | pass |
| “cat” only | “cat” only | “cat” only | pass |
| concatenate cat | concatenate dog | concatenate dog | pass |
| “a cat here” cat “another cat” | “a cat here” dog “another cat” | “a cat here” dog “another cat” | pass |
Attempt A: no protection and no word boundary.
check_replace("cat", ss, ss_expected, "dog")| input | expected | got | verdict |
|---|---|---|---|
| cat “cat” cat | dog “cat” dog | dog “dog” dog | FAIL |
| a cat sat | a dog sat | a dog sat | pass |
| “cat” only | “cat” only | “dog” only | FAIL |
| concatenate cat | concatenate dog | condogenate dog | FAIL |
| “a cat here” cat “another cat” | “a cat here” dog “another cat” | “a dog here” dog “another dog” | FAIL |
cat is replaced inside quotes, and inside concatenate, which becomes condogenate.
Attempt B: a function that always answers dog.
always_dog <- function(m) rep("dog", length(m)) # one "dog" per match
check_replace('"[^"]*"|\\bcat\\b', ss, ss_expected, always_dog)| input | expected | got | verdict |
|---|---|---|---|
| cat “cat” cat | dog “cat” dog | dog dog dog | FAIL |
| a cat sat | a dog sat | a dog sat | pass |
| “cat” only | “cat” only | dog only | FAIL |
| concatenate cat | concatenate dog | concatenate dog | pass |
| “a cat here” cat “another cat” | “a cat here” dog “another cat” | dog dog dog | FAIL |
The quoted section was matched correctly, but the replacement never checks which branch matched, so the whole quoted section is replaced by dog. Consuming the region is only half the technique; the function must hand it back unchanged.
Note the rep("dog", length(m)). The shorter-looking function(m) "dog" returns a single value for all the matches, and stringr refuses it:
tryCatch(
str_replace_all(ss, '"[^"]*"|\\bcat\\b', function(m) "dog"),
error = function(e) cat(conditionMessage(e))
)`replacement` function must return a vector the same length as the input
(9), not length 1.
Attempt C: greedy ".*".
check_replace('".*"|\\bcat\\b', ss, ss_expected, keep_or_dog)| input | expected | got | verdict |
|---|---|---|---|
| cat “cat” cat | dog “cat” dog | dog “cat” dog | pass |
| a cat sat | a dog sat | a dog sat | pass |
| “cat” only | “cat” only | “cat” only | pass |
| concatenate cat | concatenate dog | concatenate dog | pass |
| “a cat here” cat “another cat” | “a cat here” dog “another cat” | “a cat here” cat “another cat” | FAIL |
Greedy .* runs from the first quote to the last quote. In the final row, the unquoted cat between two quoted sections is swallowed as if it were inside quotes. Use [^"]* to stop at the next quote.
Attempt D: a scalar if inside the function.
scalar_if <- function(m) {
if (str_starts(m, '"')) m else "dog" # only looks at ONE value
}
tryCatch(
check_replace('"[^"]*"|\\bcat\\b', ss, ss_expected, scalar_if),
error = function(e) cat(conditionMessage(e))
)| input | expected | got | verdict |
|---|---|---|---|
| cat “cat” cat | dog “cat” dog | ERROR: Failed to apply replacement function. ℹ It must accept a character vector of any length. Caused by error in if (str_starts(m, "\"")) ...: ! the condition has length > 1 |
FAIL |
| a cat sat | a dog sat | ERROR: Failed to apply replacement function. ℹ It must accept a character vector of any length. Caused by error in if (str_starts(m, "\"")) ...: ! the condition has length > 1 |
FAIL |
| “cat” only | “cat” only | ERROR: Failed to apply replacement function. ℹ It must accept a character vector of any length. Caused by error in if (str_starts(m, "\"")) ...: ! the condition has length > 1 |
FAIL |
| concatenate cat | concatenate dog | ERROR: Failed to apply replacement function. ℹ It must accept a character vector of any length. Caused by error in if (str_starts(m, "\"")) ...: ! the condition has length > 1 |
FAIL |
| “a cat here” cat “another cat” | “a cat here” dog “another cat” | ERROR: Failed to apply replacement function. ℹ It must accept a character vector of any length. Caused by error in if (str_starts(m, "\"")) ...: ! the condition has length > 1 |
FAIL |
if needs a single TRUE or FALSE, but m holds every match at once. Depending on your R version this is an error or a silent use of only the first match. The vectorized ifelse() is the fix.
2.15 Challenge 13: Thousands separators again (consume first)
Task. This is Challenge 3 with a different technique: add commas to the integer part of each number, leave the decimals alone, and do not use a lookbehind to guard the decimals. Reuse nums and expected3 from Challenge 3. Remember that a function replacement must return one value per match (see Challenge 12).
Your turn
my_pattern <- ""
my_replacement <- function(m) m # write the replacement logic here (vectorized!)
check_replace(my_pattern, nums, expected3, my_replacement)add_commas <- function(m) {
ifelse(str_starts(m, fixed(".")), m,
str_replace_all(m, "(?<=\\d)(?=(\\d{3})+$)", ","))
}
check_replace("\\.\\d+|\\d+", nums, expected3, add_commas)| input | expected | got | verdict |
|---|---|---|---|
| 1234567.891234 | 1,234,567.891234 | 1,234,567.891234 | pass |
| 12345.6789 | 12,345.6789 | 12,345.6789 | pass |
| 1000 | 1,000 | 1,000 | pass |
| 999 | 999 | 999 | pass |
| 0.123456 | 0.123456 | 0.123456 | pass |
| 123456.123456 | 123,456.123456 | 123,456.123456 | pass |
Two branches again:
\.\d+matches the decimal part (a dot and its digits) and hands it back unchanged.\d+matches a run of digits that is not after a dot, which is the integer part.
Inside the function, the classic comma pattern is now safe, because it only ever sees a pure run of digits. That is why it can use $ and needs no decimal guard. Both ifelse() and str_replace_all() are vectorized, so the function returns one value per match. Compare this with Challenge 3: the result is the same, but the decimal part was handled by consuming it rather than vetoing commas inside it.
Attempt A: no decimal branch.
check_replace("\\d+", nums, expected3, function(m) str_replace_all(m, "(?<=\\d)(?=(\\d{3})+$)", ","))| input | expected | got | verdict |
|---|---|---|---|
| 1234567.891234 | 1,234,567.891234 | 1,234,567.891,234 | FAIL |
| 12345.6789 | 12,345.6789 | 12,345.6,789 | FAIL |
| 1000 | 1,000 | 1,000 | pass |
| 999 | 999 | 999 | pass |
| 0.123456 | 0.123456 | 0.123,456 | FAIL |
| 123456.123456 | 123,456.123456 | 123,456.123,456 | FAIL |
Without a branch that swallows the decimals, the digits after the dot are just another run of digits and get commas.
Attempt B: str_starts() with a bare ..
wrong_dot <- function(m) {
ifelse(str_starts(m, "."), m, str_replace_all(m, "(?<=\\d)(?=(\\d{3})+$)", ","))
}
check_replace("\\.\\d+|\\d+", nums, expected3, wrong_dot)| input | expected | got | verdict |
|---|---|---|---|
| 1234567.891234 | 1,234,567.891234 | 1234567.891234 | FAIL |
| 12345.6789 | 12,345.6789 | 12345.6789 | FAIL |
| 1000 | 1,000 | 1000 | FAIL |
| 999 | 999 | 999 | pass |
| 0.123456 | 0.123456 | 0.123456 | pass |
| 123456.123456 | 123,456.123456 | 123456.123456 | FAIL |
The pattern argument of str_starts() is a regex too, and . matches any character. Every token “starts with a dot”, so the function returns everything unchanged and nothing is formatted. Use fixed(".") or "\\.".
Attempt C: the whole number as one token.
whole_dot <- function(m) {
ifelse(str_starts(m, fixed(".")), m, str_replace_all(m, "(?<=\\d)(?=(\\d{3})+$)", ","))
}
check_replace("\\d+\\.?\\d*", nums, expected3, whole_dot)| input | expected | got | verdict |
|---|---|---|---|
| 1234567.891234 | 1,234,567.891234 | 1234567.891,234 | FAIL |
| 12345.6789 | 12,345.6789 | 12345.6,789 | FAIL |
| 1000 | 1,000 | 1,000 | pass |
| 999 | 999 | 999 | pass |
| 0.123456 | 0.123456 | 0.123,456 | FAIL |
| 123456.123456 | 123,456.123456 | 123456.123,456 | FAIL |
Here the unwanted region is not separated out. The token 1234567.891234 is one match that does not start with a dot, so the function treats it as a single digit run and the $ rule counts from the end of the decimals. Always give the region its own branch.
Veto or consume? Use a veto (lookaround) when the region has a clean landmark and a bounded reach, such as “right after this character”. Use consumption when the region is easy to match as a whole (a quoted string, a decimal part, a comment), or when lookbehind’s length limit gets in the way.
2.16 The general rule for lookarounds
Almost every challenge above falls into one of a handful of families. Decide which family you are in first; the pattern follows from that.
| If the English says… | Use | Shape |
|---|---|---|
| “contains X somewhere” (unordered) | lookahead from the start | ^(?=.*X) |
| “contains X and Y and Z” (unordered) | stacked lookaheads | ^(?=.*X)(?=.*Y)(?=.*Z) |
| “does not contain X anywhere” | negative lookahead | ^(?!.*X) |
| “length between a and b” | lookahead with $ |
^(?=.{a,b}$) |
| “X, then later Y” (ordered) | consuming chain | .*X.*Y |
| “same thing twice” | backreference | (\w+)\s+\1 |
| “insert / split / only at a certain spot” | zero-width lookarounds | (?<=A)(?=B) |
| “no X before the first Y” | tempered scan | ^(?:(?!X).)*Y |
| “X right after a label, without the label” | lookbehind (bounded) | (?<=label\s{0,3})X |
| “the string must not end with X” | negative lookbehind or lookahead | (?<!X)$ or (?!.*X$) |
| “X, but not inside (…)” | negative lookahead, scoped | X(?![^(]*\)) |
| “X, but not inside quotes” | parity lookahead | X(?=(?:[^"]*"[^"]*")*[^"]*$) |
| “X, but not inside an easy-to-match region” | consume it first, then a vectorized function | REGION\|X |
2.16.1 The recipe
For checking whole strings with str_detect():
^ (?=.*MUST1) (?=.*MUST2) ... (?!.*FORBID1) ... [ordered chain, if any]
| Piece | Reads as |
|---|---|
^ |
start at position 0, so every check runs from the same place |
(?=.*X) |
X appears somewhere |
(?!.*X) |
X appears nowhere |
(?=.{min,max}$) |
total length is between min and max |
.*A.*B (consuming) |
A appears, and B appears later |
Worked example. “Contains cat and dog in any order, no bird, at most 20 characters”:
animals <- c("cat and dog", "dog, cat", "cat only", "cat dog bird",
"cat and dog and a very long tail")
animals_expected <- c(TRUE, TRUE, FALSE, FALSE, FALSE)
rule_demo <- "^(?=.{1,20}$)(?=.*cat)(?=.*dog)(?!.*bird)"
check_detect(rule_demo, animals, animals_expected)| input | expected | got | verdict |
|---|---|---|---|
| cat and dog | TRUE | TRUE | pass |
| dog, cat | TRUE | TRUE | pass |
| cat only | FALSE | FALSE | pass |
| cat dog bird | FALSE | FALSE | pass |
| cat and dog and a very long tail | FALSE | FALSE | pass |
Each requirement became one lookahead, and the ^ made them all start at the same place.
2.16.2 Why the ^ matters
Without the anchor, the engine tries the pattern at every starting position. A positive lookahead is safe (if it succeeds later, it also succeeds at position 0, because the rest of the string is a subset of the whole string). A negative lookahead is not: it can succeed late in the string, after the forbidden word has already gone past.
str_detect("cat bird", "(?!.*bird)") # TRUE: succeeds at the very end of the string[1] TRUE
str_detect("cat bird", "^(?!.*bird)") # FALSE: the anchored version is correct[1] FALSE
2.17 Lookaround pitfall checklist
Before trusting a pattern, run through this list.
- Anchored? Start with
^so the zero-width checks all run from position 0. - Length bounded on both sides?
{8,20}needs$after it to mean “at most 20”. - Flag scope? A global
(?i)also affects[A-Z]. Use(?i:...)for only the part that needs it. - Lookbehind bounded?
*and+inside a lookbehind are errors;{0,n}works but has a blind spot beyond n. - Backtracking?
\d+(?!x)can shrink to a shorter match to satisfy the lookahead. Fence both sides with the same character class. - Ordered or unordered? Lookaheads stacked at one position are unordered. If order matters, use consuming
.*. - Negation direction?
(?!.*A.*B)means “no A is ever followed by B”, which is stronger than “this B has no A before it”. - Scoped lookahead?
.*inside a lookahead looks all the way to the end of the string. To confine a check to one word or one group, use a class that stops at the boundary:\w*for a word,[^(]*for “before the next parenthesis”. - Test the boundaries. Include inputs that are just inside and just outside every rule: too short, too long, wrong order, wrong case, the forbidden word at the very end.
- Do your delimiters behave? The bracket and quote techniques assume balanced, non-nested pairs and no escaped quotes. When the input breaks that, switch to a parser.
- Did the function check which branch matched? With a
REGION|Xpattern and a function replacement, the function must hand the region back unchanged, andstr_starts()orstr_detect()inside it needsfixed()or an escaped dot. - Is the replacement function vectorized?
str_replace_all()calls a function replacement once with all the matches and needs a vector of the same length back. Useifelse()andrep(value, length(m)); a bare"dog"or a scalarifbreaks it.
2.18 Extra practice: lookarounds
Short problems with the solution hidden. As before, write a pattern first.
2.18.1 Words containing all five vowels
Extract every word that contains all five vowels (a, e, i, o, u), in any order.
vtxt <- "education is a key; sequoia is not a cat; the facetious dog barks."
vexpected <- "education, sequoia, facetious"sol_vowels <- "\\b(?=\\w*a)(?=\\w*e)(?=\\w*i)(?=\\w*o)(?=\\w*u)\\w+"
check_extract_all(sol_vowels, vtxt, vexpected)| input | expected | got | verdict |
|---|---|---|---|
| education is a key; sequoia is not a cat; the facetious dog barks. | education, sequoia, facetious | education, sequoia, facetious | pass |
This is the unordered-AND recipe from the general rule, applied inside one word: \b starts at a word boundary, five stacked lookaheads each check for one vowel, and \w+ returns the word. The lookaheads use \w* rather than .* so they cannot look past the end of the word. Two near-misses:
# .* looks ahead through the rest of the whole string, not just this word
check_extract_all("\\b(?=.*a)(?=.*e)(?=.*i)(?=.*o)(?=.*u)\\w+", vtxt, vexpected)| input | expected | got | verdict |
|---|---|---|---|
| education is a key; sequoia is not a cat; the facetious dog barks. | education, sequoia, facetious | education, is, a, key, sequoia, is, not, a, cat, the, facetious | FAIL |
# a consuming chain requires the vowels in the order a, e, i, o, u
check_extract_all("\\b\\w*a\\w*e\\w*i\\w*o\\w*u\\w*", vtxt, vexpected)| input | expected | got | verdict |
|---|---|---|---|
| education is a key; sequoia is not a cat; the facetious dog barks. | education, sequoia, facetious | facetious | FAIL |
With .*, every word that has the five vowels somewhere later in the text qualifies, which is nearly all of them. The chain keeps only facetious, the one word whose vowels happen to appear in alphabetical order.
Write your own
Pick a rule from something you work with (file names, IDs, log lines, dates, survey responses). State it in plain English, classify each part with the table in the general rule, build the pattern, and write at least two failing tests that a near-miss would pass. Finding the near-miss is the real skill.
3 Part II. From pattern to pipeline
Part I went deep on one idea. This part covers the whole stringr toolkit. The challenge layout is the same, and the numbering continues so the two parts stay one workbook.
3.1 The map of stringr
3.1.1 Function families
Nearly every function is named str_<verb>(), takes the string vector first, and the pattern second, so everything works in a pipe.
| Task | Functions | Returns |
|---|---|---|
| Does it match? | str_detect(), str_starts(), str_ends() |
logical vector |
| Which ones? | str_which(), str_subset() |
positions / the matching strings |
| How many matches? | str_count() |
integer vector |
| Where? | str_locate(), str_locate_all() |
matrix of start / end |
| Pull text out | str_extract(), str_extract_all() |
character vector / list |
| Pull text out, by group | str_match(), str_match_all() |
matrix / list of matrices |
| Replace or delete | str_replace(), str_replace_all(), str_remove(), str_remove_all() |
character vector |
| Split | str_split(), str_split_1(), str_split_fixed(), str_split_i() |
list / vector / matrix / vector |
| Slice by position | str_sub() (also str_sub<-), str_trunc() |
character vector |
| Reshape | str_pad(), str_trim(), str_squish(), str_wrap(), str_dup() |
character vector |
| Case | str_to_upper(), str_to_lower(), str_to_title(), str_to_sentence() |
character vector |
| Combine | str_c(), str_flatten(), str_flatten_comma(), str_glue() |
character vector |
| Sort and compare | str_sort(), str_order(), str_rank(), str_unique(), str_equal() |
various |
| Missing values | str_replace_na() |
character vector |
| Debug | str_view() |
HTML view of matches |
Two naming rules explain a lot:
_allmeans every match. Without it you get only the first match (str_replacevsstr_replace_all,str_extractvsstr_extract_all).- A bare function is vectorized over the input, so one call handles a whole column.
3.1.2 A quick tour
Run each line and read the result. Note what happens to the NA.
fruit <- c("apple", "Banana", NA, "cherry pie")
str_length(fruit)[1] 5 6 NA 10
str_detect(fruit, "an")[1] FALSE TRUE NA FALSE
str_detect(fruit, "an", negate = TRUE)[1] TRUE FALSE NA TRUE
str_which(fruit, "an")[1] 2
str_subset(fruit, "an")[1] "Banana"
str_starts(fruit, "a")[1] TRUE FALSE NA FALSE
str_ends(fruit, "pie")[1] FALSE FALSE NA TRUE
str_count(fruit, "[aeiou]")[1] 2 3 NA 3
str_extract(fruit, "[aeiou]+")[1] "a" "a" NA "e"
str_sub(fruit, 1, 3)[1] "app" "Ban" NA "che"
str_sub(fruit, -3, -1)[1] "ple" "ana" NA "pie"
str_to_upper(fruit)[1] "APPLE" "BANANA" NA "CHERRY PIE"
str_pad(fruit, 12, side = "both", pad = "-")[1] "---apple----" "---Banana---" NA "-cherry pie-"
str_trunc(fruit, 8)[1] "apple" "Banana" NA "cherr..."
str_remove(fruit, "[aeiou]")[1] "pple" "Bnana" NA "chrry pie"
str_replace_na(fruit, "<missing>")[1] "apple" "Banana" "<missing>" "cherry pie"
Two things to notice. First, NA in means NA out: stringr does not guess. Second, some functions return something other than a vector. str_locate() returns a matrix, str_locate_all() a list of matrices, and str_split_fixed() a matrix with a fixed number of columns:
str_locate(c("hello world", "no match"), "o") start end
[1,] 5 5
[2,] 2 2
str_locate_all("hello world", "o")[[1]]
start end
[1,] 5 5
[2,] 8 8
str_split_fixed(c("a-b-c", "d-e"), "-", 2) [,1] [,2]
[1,] "a" "b-c"
[2,] "d" "e"
And the remaining verbs, in one go:
str_to_lower("ABC")[1] "abc"
str_to_sentence("hELLO wORLD")[1] "Hello world"
str_dup("ab", 3)[1] "ababab"
str_unique(c("a", "A", "a"))[1] "a" "A"
str_equal("a", "A", ignore_case = TRUE)[1] TRUE
str_rank(c("banana", "apple", "cherry"))[1] 2 1 3
cat(str_wrap("the quick brown fox jumps over the lazy dog", width = 15))the quick brown
fox jumps over
the lazy dog
3.1.3 Four ways to read a pattern
A pattern is a regular expression by default. Wrap it to change how it is read:
| Wrapper | Meaning | Use when |
|---|---|---|
(none) / regex() |
regular expression | you need structure |
fixed() |
exact bytes, no special characters | you want literal text, fast |
coll() |
locale-aware comparison | accents and language-specific rules matter |
boundary() |
text boundaries ("character", "word", "sentence", "line_break") |
you split or count by linguistic units |
regex() also takes modifiers: ignore_case, multiline (^ and $ match at every line), dotall (. also matches newlines), and comments (whitespace and # comments allowed inside the pattern).
To see what a pattern does while you build it, use str_view():
str_view(c("apple pie", "banana"), "an")3.2 Finding and counting
3.2.1 Challenge 14: Literal text with special characters
Task. Detect strings containing the literal text (1+1), parentheses and plus sign included.
lit <- c("calc (1+1) now", "calc 1+1 now", "11", "111 apples", "(1+1", "x(1+1)y")
lit_expected <- c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE)Your turn
my_answer <- function(x) str_detect(x, "") # write your pattern here
check_fn(my_answer, lit, lit_expected)check_detect(fixed("(1+1)"), lit, lit_expected)| input | expected | got | verdict |
|---|---|---|---|
| calc (1+1) now | TRUE | TRUE | pass |
| calc 1+1 now | FALSE | FALSE | pass |
| 11 | FALSE | FALSE | pass |
| 111 apples | FALSE | FALSE | pass |
| (1+1 | FALSE | FALSE | pass |
| x(1+1)y | TRUE | TRUE | pass |
fixed() turns every special character off, which is the simplest way to say “this exact text”. The alternative is to escape each special character yourself, or to let stringr do it:
check_detect("\\(1\\+1\\)", lit, lit_expected)| input | expected | got | verdict |
|---|---|---|---|
| calc (1+1) now | TRUE | TRUE | pass |
| calc 1+1 now | FALSE | FALSE | pass |
| 11 | FALSE | FALSE | pass |
| 111 apples | FALSE | FALSE | pass |
| (1+1 | FALSE | FALSE | pass |
| x(1+1)y | TRUE | TRUE | pass |
str_escape("(1+1)")[1] "\\(1\\+1\\)"
Attempt A: no escaping.
check_detect("(1+1)", lit, lit_expected)| input | expected | got | verdict |
|---|---|---|---|
| calc (1+1) now | TRUE | FALSE | FAIL |
| calc 1+1 now | FALSE | FALSE | pass |
| 11 | FALSE | TRUE | FAIL |
| 111 apples | FALSE | TRUE | FAIL |
| (1+1 | FALSE | FALSE | pass |
| x(1+1)y | TRUE | FALSE | FAIL |
In a regex, (1+1) is a group containing “one or more 1s, then a 1”. It matches 11 and 111, and never matches the real text (1+1), because the + is a quantifier, not a character.
Attempt B: escaping the parentheses only.
check_detect("\\(1+1\\)", lit, lit_expected)| input | expected | got | verdict |
|---|---|---|---|
| calc (1+1) now | TRUE | FALSE | FAIL |
| calc 1+1 now | FALSE | FALSE | pass |
| 11 | FALSE | FALSE | pass |
| 111 apples | FALSE | FALSE | pass |
| (1+1 | FALSE | FALSE | pass |
| x(1+1)y | TRUE | FALSE | FAIL |
The parentheses are now literal, but 1+1 still means “one or more 1s, then a 1”, so the pattern looks for (11), (111), and so on.
Attempt C: over-escaping.
check_detect("\\\\(1\\\\+1\\\\)", lit, lit_expected)| input | expected | got | verdict |
|---|---|---|---|
| calc (1+1) now | TRUE | FALSE | FAIL |
| calc 1+1 now | FALSE | FALSE | pass |
| 11 | FALSE | FALSE | pass |
| 111 apples | FALSE | FALSE | pass |
| (1+1 | FALSE | FALSE | pass |
| x(1+1)y | TRUE | FALSE | FAIL |
In an R string, "\\\\" is two characters, \\, which in a regex means “a literal backslash”. The pattern now searches for text containing backslashes. One backslash per layer: "\\(" in R becomes \( for the regex engine.
3.2.2 Challenge 15: Lines that start with a label (multiline text)
Task. The string below contains five lines separated by \n. Use str_extract_all() to pull out every whole line that starts with ERROR:. The line NOTERROR: ... must not count.
log2 <- paste("INFO: start", "ERROR: disk full", "NOTERROR: ignore me",
"INFO: retry", "ERROR: timeout", sep = "\n")
lines_expected <- "ERROR: disk full, ERROR: timeout"Your turn
my_pattern <- "" # write your pattern here (hint: regex() has modifiers)
check_extract_all(my_pattern, log2, lines_expected)sol_lines <- regex("^ERROR:.*$", multiline = TRUE)
check_extract_all(sol_lines, log2, lines_expected)| input | expected | got | verdict |
|---|---|---|---|
| INFO: start |
ERROR: disk full NOTERROR: ignore me INFO: retry ERROR: timeout |ERROR: disk full, ERROR: timeout |ERROR: disk full, ERROR: timeout |pass |
By default ^ and $ match only the start and end of the whole string. With multiline = TRUE they match at the start and end of every line. The .* stops at the end of each line because . does not match a newline by default.
An equally good approach is to split first, then filter:
check_value(str_subset(str_split_1(log2, "\n"), "^ERROR:"),
c("ERROR: disk full", "ERROR: timeout"))| expected | got | verdict |
|---|---|---|
| c(“ERROR: disk full”, “ERROR: timeout”) | c(“ERROR: disk full”, “ERROR: timeout”) | pass |
Attempt A: anchors without multiline.
check_extract_all("^ERROR:.*$", log2, lines_expected)| input | expected | got | verdict |
|---|---|---|---|
| INFO: start |
ERROR: disk full NOTERROR: ignore me INFO: retry ERROR: timeout |ERROR: disk full, ERROR: timeout |(nothing) |FAIL |
The string begins with INFO:, so ^ERROR: fails at the only place ^ can match.
Attempt B: no anchors at all.
check_extract_all("ERROR:.*", log2, lines_expected)| input | expected | got | verdict |
|---|---|---|---|
| INFO: start |
ERROR: disk full NOTERROR: ignore me INFO: retry ERROR: timeout |ERROR: disk full, ERROR: timeout |ERROR: disk full, ERROR: ignore me, ERROR: timeout |FAIL |
This looks like it works, until the NOTERROR: line, which contains the text ERROR: in the middle. Without ^, a match can begin anywhere.
Attempt C: adding dotall.
check_extract_all(regex("^ERROR:.*$", multiline = TRUE, dotall = TRUE), log2, lines_expected)| input | expected | got | verdict |
|---|---|---|---|
| INFO: start |
ERROR: disk full NOTERROR: ignore me INFO: retry ERROR: timeout |ERROR: disk full, ERROR: timeout |ERROR: disk full NOTERROR: ignore me INFO: retry ERROR: timeout |FAIL |
dotall = TRUE lets . match newlines too, so .* runs from the first ERROR: to the end of the string and returns one huge match. multiline and dotall control different things: anchors versus the dot.
3.2.3 Challenge 16: Greedy versus lazy
Task. Extract the text inside every <b>...</b> tag. One of the pieces contains a space and one is a number.
tags3 <- "<b>one</b> and <b>big deal</b> and <b>3</b>"
tags_expected <- "one, big deal, 3"Your turn
my_pattern <- "" # write your pattern here
check_extract_all(my_pattern, tags3, tags_expected)check_extract_all("(?<=<b>).*?(?=</b>)", tags3, tags_expected)| input | expected | got | verdict |
|---|---|---|---|
| one and big deal and 3 | one, big deal, 3 | one, big deal, 3 | pass |
.*? is the lazy version of .*: it takes as little as possible, so it stops at the first </b>. The surrounding lookbehind and lookahead check the tags without including them in the result.
Two other correct approaches:
# capture group with str_match_all(): column 2 holds the group
check_value(str_match_all(tags3, "<b>(.*?)</b>")[[1]][, 2],
c("one", "big deal", "3"))| expected | got | verdict |
|---|---|---|
| c(“one”, “big deal”, “3”) | c(“one”, “big deal”, “3”) | pass |
# negated class: anything that is not "<"
check_extract_all("(?<=<b>)[^<]+(?=</b>)", tags3, tags_expected)| input | expected | got | verdict |
|---|---|---|---|
| one and big deal and 3 | one, big deal, 3 | one, big deal, 3 | pass |
Attempt A: greedy .*.
check_extract_all("(?<=<b>).*(?=</b>)", tags3, tags_expected)| input | expected | got | verdict |
|---|---|---|---|
| one and big deal and 3 | one, big deal, 3 | one and big deal and 3 | FAIL |
Greedy .* takes as much as it can and then backs off only until the lookahead is satisfied, so it stops at the last </b>, not the first.
Attempt B: \w+ for the content.
check_extract_all("(?<=<b>)\\w+(?=</b>)", tags3, tags_expected)| input | expected | got | verdict |
|---|---|---|---|
| one and big deal and 3 | one, big deal, 3 | one, 3 | FAIL |
\w+ cannot match a space, so big deal is not followed directly by </b> and is skipped. Assuming what the content looks like is the usual weakness of narrow classes.
3.2.4 Challenge 17: Phone numbers (a readable pattern)
Task. Detect valid US phone numbers: ten digits, optionally written with parentheses around the area code, and with an optional space, dot, or hyphen between the three groups. The parentheses must come as a pair. Write the pattern with regex(..., comments = TRUE) so it can be read like a recipe.
ph <- c("(202) 555-0147", "202-555-0147", "202.555.0147", "2025550147",
"(202)555-0147", # valid: no space
"(202 555-0147", "202) 555-0147", # unbalanced parentheses
"555-0147", # too short
"12025550147", # eleven digits
"202x555x0147") # wrong separators
ph_expected <- c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)Your turn
my_answer <- function(x) str_detect(x, "") # write your pattern here
check_fn(my_answer, ph, ph_expected)sol_phone <- regex("
^
(?: \\( \\d{3} \\) # area code in parentheses ...
| \\d{3} ) # ... or without them
[\\s.-]? # optional separator: space, dot or hyphen
\\d{3} # exchange
[\\s.-]? # optional separator
\\d{4} # line number
$
", comments = TRUE)
check_detect(sol_phone, ph, ph_expected)| input | expected | got | verdict |
|---|---|---|---|
| (202) 555-0147 | TRUE | TRUE | pass |
| 202-555-0147 | TRUE | TRUE | pass |
| 202.555.0147 | TRUE | TRUE | pass |
| 2025550147 | TRUE | TRUE | pass |
| (202)555-0147 | TRUE | TRUE | pass |
| (202 555-0147 | FALSE | FALSE | pass |
| 202) 555-0147 | FALSE | FALSE | pass |
| 555-0147 | FALSE | FALSE | pass |
| 12025550147 | FALSE | FALSE | pass |
| 202x555x0147 | FALSE | FALSE | pass |
With comments = TRUE, whitespace in the pattern is ignored and # starts a comment, so you can lay the pattern out in pieces. The alternation \(\d{3}\) | \d{3} ties the two parentheses together: either both or neither. Inside [...], put - last so it is literal.
Attempt A: each parenthesis optional on its own.
check_detect("^\\(?\\d{3}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}$", ph, ph_expected)| input | expected | got | verdict |
|---|---|---|---|
| (202) 555-0147 | TRUE | TRUE | pass |
| 202-555-0147 | TRUE | TRUE | pass |
| 202.555.0147 | TRUE | TRUE | pass |
| 2025550147 | TRUE | TRUE | pass |
| (202)555-0147 | TRUE | TRUE | pass |
| (202 555-0147 | FALSE | TRUE | FAIL |
| 202) 555-0147 | FALSE | TRUE | FAIL |
| 555-0147 | FALSE | FALSE | pass |
| 12025550147 | FALSE | FALSE | pass |
| 202x555x0147 | FALSE | FALSE | pass |
\(? and \)? are independent, so a lone ( or a lone ) is accepted. “Both or neither” needs the alternation.
Attempt B: . as the separator.
check_detect("^(?:\\(\\d{3}\\)|\\d{3}).?\\d{3}.?\\d{4}$", ph, ph_expected)| input | expected | got | verdict |
|---|---|---|---|
| (202) 555-0147 | TRUE | TRUE | pass |
| 202-555-0147 | TRUE | TRUE | pass |
| 202.555.0147 | TRUE | TRUE | pass |
| 2025550147 | TRUE | TRUE | pass |
| (202)555-0147 | TRUE | TRUE | pass |
| (202 555-0147 | FALSE | FALSE | pass |
| 202) 555-0147 | FALSE | FALSE | pass |
| 555-0147 | FALSE | FALSE | pass |
| 12025550147 | FALSE | TRUE | FAIL |
| 202x555x0147 | FALSE | TRUE | FAIL |
An unescaped . is a wildcard, so x is accepted as a separator, and with eleven digits the first .? simply swallows a digit.
Attempt C: no anchors.
check_detect("(?:\\(\\d{3}\\)|\\d{3})[\\s.-]?\\d{3}[\\s.-]?\\d{4}", ph, ph_expected)| input | expected | got | verdict |
|---|---|---|---|
| (202) 555-0147 | TRUE | TRUE | pass |
| 202-555-0147 | TRUE | TRUE | pass |
| 202.555.0147 | TRUE | TRUE | pass |
| 2025550147 | TRUE | TRUE | pass |
| (202)555-0147 | TRUE | TRUE | pass |
| (202 555-0147 | FALSE | TRUE | FAIL |
| 202) 555-0147 | FALSE | FALSE | pass |
| 555-0147 | FALSE | FALSE | pass |
| 12025550147 | FALSE | TRUE | FAIL |
| 202x555x0147 | FALSE | FALSE | pass |
Without ^ and $, any substring of a longer string can match. A validation pattern must be anchored.
Attempt D: forgetting to escape the parentheses.
check_detect("^(?:(\\d{3})|\\d{3})[\\s.-]?\\d{3}[\\s.-]?\\d{4}$", ph, ph_expected)| input | expected | got | verdict |
|---|---|---|---|
| (202) 555-0147 | TRUE | FALSE | FAIL |
| 202-555-0147 | TRUE | TRUE | pass |
| 202.555.0147 | TRUE | TRUE | pass |
| 2025550147 | TRUE | TRUE | pass |
| (202)555-0147 | TRUE | FALSE | FAIL |
| (202 555-0147 | FALSE | FALSE | pass |
| 202) 555-0147 | FALSE | FALSE | pass |
| 555-0147 | FALSE | FALSE | pass |
| 12025550147 | FALSE | FALSE | pass |
| 202x555x0147 | FALSE | FALSE | pass |
Unescaped ( ) form a group, not literal parentheses, so numbers that actually contain parentheses are rejected.
Attempt E: an invalid character class.
tryCatch(str_detect(ph, "^[.-\\s]"), error = function(e) cat(conditionMessage(e)))Syntax error in regex pattern. (U_REGEX_RULE_SYNTAX, context=`^[.-\s]`)
Inside a class, .-\\s reads as a range from . to \\s, which makes no sense. Move the hyphen to the end: [\\s.-].
3.2.5 Challenge 18: Counting words
Task. Count the words in each string with str_count(). A word is a run of letters, which may contain apostrophes or hyphens between letters (Don't, well-known). Numbers are not words.
wd <- c("Don't stop-believing, ok?", "It's 5 o'clock", "well-known well-known", "", "a-b-c")
wd_expected <- c(3, 2, 2, 0, 1)Your turn
my_answer <- function(x) str_count(x, "") # write your pattern here
check_fn(my_answer, wd, wd_expected)check_count("[A-Za-z]+(?:['-][A-Za-z]+)*", wd, wd_expected)| input | expected | got | verdict |
|---|---|---|---|
| Don’t stop-believing, ok? | 3 | 3 | pass |
| It’s 5 o’clock | 2 | 2 | pass |
| well-known well-known | 2 | 2 | pass |
| 0 | 0 | pass | |
| a-b-c | 1 | 1 | pass |
The pattern says: letters, then zero or more groups of (apostrophe or hyphen, then more letters). That keeps stop-believing as one word while refusing a stray - or '.
Attempt A: \w+.
check_count("\\w+", wd, wd_expected)| input | expected | got | verdict |
|---|---|---|---|
| Don’t stop-believing, ok? | 3 | 5 | FAIL |
| It’s 5 o’clock | 2 | 5 | FAIL |
| well-known well-known | 2 | 4 | FAIL |
| 0 | 0 | pass | |
| a-b-c | 1 | 3 | FAIL |
\w includes digits and underscores, and stops at every apostrophe and hyphen, so Don't becomes Don and t.
Attempt B: boundary("word").
check_count(boundary("word"), wd, wd_expected)| input | expected | got | verdict |
|---|---|---|---|
| Don’t stop-believing, ok? | 3 | 4 | FAIL |
| It’s 5 o’clock | 2 | 3 | FAIL |
| well-known well-known | 2 | 4 | FAIL |
| 0 | 0 | pass | |
| a-b-c | 1 | 3 | FAIL |
Linguistic word boundaries keep Don't together but split at hyphens, and they count 5 as a word. They answer a different question from the one we asked.
Attempt C: letters and apostrophes only.
check_count("[A-Za-z']+", wd, wd_expected)| input | expected | got | verdict |
|---|---|---|---|
| Don’t stop-believing, ok? | 3 | 4 | FAIL |
| It’s 5 o’clock | 2 | 2 | pass |
| well-known well-known | 2 | 4 | FAIL |
| 0 | 0 | pass | |
| a-b-c | 1 | 3 | FAIL |
Close: apostrophes are handled, but stop-believing still counts as two words. Always test the separator characters your definition has to deal with.
3.3 Pulling pieces out
3.3.1 Challenge 19: Parsing key=value pairs
Task. Turn the string into a named character vector, one element per pair. Values may contain spaces; pairs are separated by ;.
kv <- "name=Ann; age=31; city=New York"
kv_expected <- c(name = "Ann", age = "31", city = "New York")Your turn
my_answer <- function(x) {
# hint: str_match_all() returns a list holding one matrix per string
}
check_value(my_answer(kv), kv_expected)parse_kv <- function(x) {
m <- str_match_all(x, "(\\w+)=([^;]+)")[[1]]
setNames(m[, 3], m[, 2])
}
check_value(parse_kv(kv), kv_expected)| expected | got | verdict |
|---|---|---|
| c(name = “Ann”, age = “31”, city = “New York”) | c(name = “Ann”, age = “31”, city = “New York”) | pass |
str_match_all() returns a matrix per string: column 1 is the whole match, then one column per capture group. Here column 2 holds the keys and column 3 the values. [^;]+ means “anything except a semicolon”, which allows spaces inside a value but stops at the separator.
When a string does not match at all, str_match() returns a row of NA:
str_match(c("a=1", "none"), "(\\w)=(\\d)") [,1] [,2] [,3]
[1,] "a=1" "a" "1"
[2,] NA NA NA
Attempt A: str_match() instead of str_match_all().
str_match(kv, "(\\w+)=([^;]+)") [,1] [,2] [,3]
[1,] "name=Ann" "name" "Ann"
Without _all, only the first match is returned. The pattern is right, the function is wrong.
Attempt B: \w+ for the value.
str_match_all(kv, "(\\w+)=(\\w+)")[[1]] [,1] [,2] [,3]
[1,] "name=Ann" "name" "Ann"
[2,] "age=31" "age" "31"
[3,] "city=New" "city" "New"
A value with a space is cut short: city=New. The value class has to allow whatever the data allows.
Attempt C: greedy .+ on both sides.
str_match_all(kv, "(.+)=(.+)")[[1]] [,1] [,2] [,3]
[1,] "name=Ann; age=31; city=New York" "name=Ann; age=31; city" "New York"
Greedy .+ swallows the separators and produces one giant match. When a delimiter separates items, forbid the delimiter with a negated class like [^;].
3.3.2 Challenge 20: Splitting messy lists
Task. Split the comma-separated list into clean items. The spacing is inconsistent: spaces before and after commas, and around the whole string.
x7 <- " red , green,blue , yellow "
list_expected <- c("red", "green", "blue", "yellow")Your turn
my_answer <- function(x) str_split_1(x, "") # write your pattern here
check_value(my_answer(x7), list_expected)check_value(str_split_1(str_trim(x7), "\\s*,\\s*"), list_expected)| expected | got | verdict |
|---|---|---|
| c(“red”, “green”, “blue”, “yellow”) | c(“red”, “green”, “blue”, “yellow”) | pass |
Two steps: trim the ends of the string, then split on a comma that absorbs the spaces around it. str_split_1() handles one string and returns a plain vector; str_split() handles many and returns a list.
Splitting loosely and cleaning each piece afterwards is equally valid:
check_value(str_trim(str_split_1(x7, ",")), list_expected)| expected | got | verdict |
|---|---|---|
| c(“red”, “green”, “blue”, “yellow”) | c(“red”, “green”, “blue”, “yellow”) | pass |
Other members of the family: str_split_fixed(x, "-", 2) gives a matrix with a fixed number of columns, and str_split_i(x, "-", 2) returns just the second piece of every string.
Attempt A: no trimming.
check_value(str_split_1(x7, "\\s*,\\s*"), list_expected)| expected | got | verdict |
|---|---|---|
| c(“red”, “green”, “blue”, “yellow”) | c(” red”, “green”, “blue”, “yellow”) | FAIL |
The pattern absorbs spaces around commas, but there is no comma at the very start or end, so " red" and "yellow " keep their spaces.
Attempt B: a plain comma.
check_value(str_split_1(x7, ","), list_expected)| expected | got | verdict |
|---|---|---|
| c(“red”, “green”, “blue”, “yellow”) | c(” red “,” green”, “blue”, ” yellow “) | FAIL |
Every item keeps its surrounding spaces.
Attempt C: ", " (comma plus one space).
check_value(str_split_1(x7, ", "), list_expected)| expected | got | verdict |
|---|---|---|
| c(“red”, “green”, “blue”, “yellow”) | c(” red “,”green,blue “,” yellow “) | FAIL |
This only splits where a comma is followed by a space, so it splits some places and not others. Real data rarely respects one fixed separator.
3.4 Rewriting text
3.4.1 Challenge 21: Reordering with backreferences
Task. Rewrite every ISO date YYYY-MM-DD as DD/MM/YYYY, leaving the rest of the text untouched.
d8 <- c("2024-05-17", "Born 2024-05-17, moved 2025-01-02.", "no date here", "1999-12-31")
dates_expected <- c("17/05/2024", "Born 17/05/2024, moved 02/01/2025.", "no date here", "31/12/1999")Your turn
my_pattern <- "" # write your pattern here
my_replacement <- "" # and your replacement
check_replace(my_pattern, d8, dates_expected, my_replacement)check_replace("(\\d{4})-(\\d{2})-(\\d{2})", d8, dates_expected, "\\3/\\2/\\1")| input | expected | got | verdict |
|---|---|---|---|
| 2024-05-17 | 17/05/2024 | 17/05/2024 | pass |
| Born 2024-05-17, moved 2025-01-02. | Born 17/05/2024, moved 02/01/2025. | Born 17/05/2024, moved 02/01/2025. | pass |
| no date here | no date here | no date here | pass |
| 1999-12-31 | 31/12/1999 | 31/12/1999 | pass |
Each ( ) captures a piece, and \\1, \\2, \\3 in the replacement paste them back in any order. str_replace_all() rewrites every date in a string.
Attempt A: str_replace() instead of str_replace_all().
check_fn(function(x) str_replace(x, "(\\d{4})-(\\d{2})-(\\d{2})", "\\3/\\2/\\1"), d8, dates_expected)| input | expected | got | verdict |
|---|---|---|---|
| 2024-05-17 | 17/05/2024 | 17/05/2024 | pass |
| Born 2024-05-17, moved 2025-01-02. | Born 17/05/2024, moved 02/01/2025. | Born 17/05/2024, moved 2025-01-02. | FAIL |
| no date here | no date here | no date here | pass |
| 1999-12-31 | 31/12/1999 | 31/12/1999 | pass |
Only the first date in each string is rewritten, and the second date in row 2 is left alone.
Attempt B: $1 syntax.
check_replace("(\\d{4})-(\\d{2})-(\\d{2})", d8, dates_expected, "$3/$2/$1")| input | expected | got | verdict |
|---|---|---|---|
| 2024-05-17 | 17/05/2024 | $3/$2/$1 | FAIL |
| Born 2024-05-17, moved 2025-01-02. | Born 17/05/2024, moved 02/01/2025. | Born $3/$2/$1, moved $3/$2/$1. | FAIL |
| no date here | no date here | no date here | pass |
| 1999-12-31 | 31/12/1999 | $3/$2/$1 | FAIL |
$1 is the convention in other languages. In stringr the reference is \\1, and $3 is plain text.
Attempt C: too many backslashes.
check_replace("(\\d{4})-(\\d{2})-(\\d{2})", d8, dates_expected, "\\\\3/\\\\2/\\\\1")| input | expected | got | verdict |
|---|---|---|---|
| 2024-05-17 | 17/05/2024 | \3/\2/\1 | FAIL |
| Born 2024-05-17, moved 2025-01-02. | Born 17/05/2024, moved 02/01/2025. | Born \3/\2/\1, moved \3/\2/\1. | FAIL |
| no date here | no date here | no date here | pass |
| 1999-12-31 | 31/12/1999 | \3/\2/\1 | FAIL |
"\\\\3" in R is \\3, which the replacement reads as “a literal backslash followed by 3”. One level of escaping gets you the group reference; two gets you the literal text.
Attempt D: no capture groups.
tryCatch(
str_replace_all(d8, "\\d{4}-\\d{2}-\\d{2}", "\\3/\\2/\\1"),
error = function(e) cat(conditionMessage(e))
)Trying to access the index that is out of bounds. (U_INDEX_OUTOFBOUNDS_ERROR)
Without ( ) there are no groups to refer to. The message is cryptic, but “index out of bounds” means exactly that.
3.4.2 Challenge 22: Swapping two letters (per-match logic)
Task. In each string, turn every a into b and every b into a, at the same time.
s9 <- c("abba cab", "bbb", "xyz")
swap_expected <- c("baab cba", "aaa", "xyz")Your turn
my_answer <- function(x) str_replace_all(x, "", "") # write your answer here
check_fn(my_answer, s9, swap_expected)swap_ab <- function(x) {
str_replace_all(x, "[ab]", function(m) c(a = "b", b = "a")[m])
}
check_fn(swap_ab, s9, swap_expected)| input | expected | got | verdict |
|---|---|---|---|
| abba cab | baab cba | baab cba | pass |
| bbb | aaa | aaa | pass |
| xyz | xyz | xyz | pass |
When the replacement depends on what was matched, pass a function. It receives all the matched pieces at once and must return one replacement for each. Here the function looks each match up in a small dictionary.
Attempt A: a named vector of replacements.
check_fn(function(x) str_replace_all(x, c("a" = "b", "b" = "a")), s9, swap_expected)| input | expected | got | verdict |
|---|---|---|---|
| abba cab | baab cba | aaaa caa | FAIL |
| bbb | aaa | aaa | pass |
| xyz | xyz | xyz | pass |
A named vector looks like a dictionary, but stringr applies the replacements one after another: first every a becomes b, then every b (including the new ones) becomes a, so everything ends up as a.
Attempt B: two calls in a row.
check_fn(function(x) str_replace_all(str_replace_all(x, "a", "b"), "b", "a"), s9, swap_expected)| input | expected | got | verdict |
|---|---|---|---|
| abba cab | baab cba | aaaa caa | FAIL |
| bbb | aaa | aaa | pass |
| xyz | xyz | xyz | pass |
The same trap in disguise. Any sequence of replacements where later steps can see earlier results will corrupt a swap.
Attempt C: groups with \\2\\1.
check_replace("(a)|(b)", s9, swap_expected, "\\2\\1")| input | expected | got | verdict |
|---|---|---|---|
| abba cab | baab cba | abba cab | FAIL |
| bbb | aaa | bbb | FAIL |
| xyz | xyz | xyz | pass |
On an a, group 1 is filled and group 2 is empty, so \\2\\1 gives "" + "a". Each letter is replaced by itself. Alternation groups do not swap; they only record which branch matched.
3.4.3 Challenge 23: Masking card numbers
Task. Replace every digit with * except the last four digits. Separators (spaces and hyphens) stay where they are. Strings with four or fewer digits are unchanged.
cn <- c("4111111111111111", "1234-5678-9012-3456", "12345", "1234", "123",
"1234 5678 9012", "1234-56-78")
mask_expected <- c("************1111", "****-****-****-3456", "*2345", "1234", "123",
"**** **** 9012", "****-56-78")Your turn
my_answer <- function(x) str_replace_all(x, "", "") # write your answer here
check_fn(my_answer, cn, mask_expected)check_replace("\\d(?=(?:\\D*\\d){4})", cn, mask_expected, "*")| input | expected | got | verdict |
|---|---|---|---|
| 4111111111111111 | ************1111 | ************1111 | pass |
| 1234-5678-9012-3456 | ****-****-****-3456 | ****-****-****-3456 | pass |
| 12345 | *2345 | *2345 | pass |
| 1234 | 1234 | 1234 | pass |
| 123 | 123 | 123 | pass |
| 1234 5678 9012 | **** **** 9012 | **** **** 9012 | pass |
| 1234-56-78 | ****-56-78 | ****-56-78 | pass |
A digit is masked when at least four more digits follow it, whatever sits between them. (?:\\D*\\d){4} reads “four times: some non-digits, then a digit”. That is the exact statement of the rule, so it works regardless of how the number is formatted.
Attempt A: four digits in a row.
check_replace("\\d(?=\\d{4})", cn, mask_expected, "*")| input | expected | got | verdict |
|---|---|---|---|
| 4111111111111111 | ************1111 | ************1111 | pass |
| 1234-5678-9012-3456 | ****-****-****-3456 | 1234-5678-9012-3456 | FAIL |
| 12345 | *2345 | *2345 | pass |
| 1234 | 1234 | 1234 | pass |
| 123 | 123 | 123 | pass |
| 1234 5678 9012 | **** **** 9012 | 1234 5678 9012 | FAIL |
| 1234-56-78 | ****-56-78 | 1234-56-78 | FAIL |
\d{4} means four consecutive digits. In 1234-5678-... no digit is followed by four digits in a row, so nothing is masked.
Attempt B: positions with str_sub() and str_pad().
mask_pad <- function(x) str_pad(str_sub(x, -4), width = str_length(x), pad = "*")
check_fn(mask_pad, cn, mask_expected)| input | expected | got | verdict |
|---|---|---|---|
| 4111111111111111 | ************1111 | ************1111 | pass |
| 1234-5678-9012-3456 | ****-****-****-3456 | ***************3456 | FAIL |
| 12345 | *2345 | *2345 | pass |
| 1234 | 1234 | 1234 | pass |
| 123 | 123 | 123 | pass |
| 1234 5678 9012 | **** **** 9012 | **********9012 | FAIL |
| 1234-56-78 | ****-56-78 | ******6-78 | FAIL |
Neat for plain digit strings. But it treats the last four characters as the last four digits, masks the separators too, and gets 1234-56-78 wrong.
Attempt C: replacing the last four digits instead of keeping them.
check_replace("\\d{4}$", cn, mask_expected, "****")| input | expected | got | verdict |
|---|---|---|---|
| 4111111111111111 | ************1111 | 411111111111**** | FAIL |
| 1234-5678-9012-3456 | ****-****-****-3456 | 1234-5678-9012-**** | FAIL |
| 12345 | *2345 | 1**** | FAIL |
| 1234 | 1234 | **** | FAIL |
| 123 | 123 | 123 | pass |
| 1234 5678 9012 | **** **** 9012 | 1234 5678 **** | FAIL |
| 1234-56-78 | ****-56-78 | 1234-56-78 | FAIL |
The rule is the reverse: mask everything except the last four.
Attempt D: .*\\d{4}.
check_replace("\\d(?=.*\\d{4})", cn, mask_expected, "*")| input | expected | got | verdict |
|---|---|---|---|
| 4111111111111111 | ************1111 | ************1111 | pass |
| 1234-5678-9012-3456 | ****-****-****-3456 | ****-****-****-3456 | pass |
| 12345 | *2345 | *2345 | pass |
| 1234 | 1234 | 1234 | pass |
| 123 | 123 | 123 | pass |
| 1234 5678 9012 | **** **** 9012 | **** **** 9012 | pass |
| 1234-56-78 | ****-56-78 | 1234-56-78 | FAIL |
Almost always right: “there are four consecutive digits somewhere later”. But 1234-56-78 has no run of four digits after its first digits, so they stay visible. The test case at the end is what exposes it.
3.5 Cleaning and assembling
3.5.1 Challenge 24: A cleaning pipeline
Task. Normalize messy names: no leading, trailing, or repeated spaces, and title case. NA must stay NA, and blank strings must become empty strings.
x11 <- c(" jOHN sMITH ", "mary-ann jones", NA, "", " ")
clean_expected <- c("John Smith", "Mary-Ann Jones", NA, "", "")Your turn
my_answer <- function(x) x # build a pipeline here
check_fn(my_answer, x11, clean_expected)clean_names <- function(x) x |> str_squish() |> str_to_title()
check_fn(clean_names, x11, clean_expected)| input | expected | got | verdict |
|---|---|---|---|
| jOHN sMITH | John Smith | John Smith | pass |
| mary-ann jones | Mary-Ann Jones | Mary-Ann Jones | pass |
| NA | NA | NA | pass |
| pass | |||
| pass |
str_squish() trims both ends and collapses internal runs of whitespace into one space, so it replaces a str_trim() plus a str_replace_all("\\s+", " "). NA passes through every step unchanged. Because the verbs are small and pipe-friendly, a cleaning step is easy to read, reorder, and test.
Attempt A: str_trim().
check_fn(function(x) x |> str_trim() |> str_to_title(), x11, clean_expected)| input | expected | got | verdict |
|---|---|---|---|
| jOHN sMITH | John Smith | John Smith | FAIL |
| mary-ann jones | Mary-Ann Jones | Mary-Ann Jones | FAIL |
| NA | NA | NA | pass |
| pass | |||
| pass |
str_trim() removes only the outer whitespace. The repeated spaces inside a name remain.
Attempt B: collapse spaces by hand.
check_fn(function(x) x |> str_replace_all("\\s+", " ") |> str_to_title(), x11, clean_expected)| input | expected | got | verdict |
|---|---|---|---|
| jOHN sMITH | John Smith | John Smith | FAIL |
| mary-ann jones | Mary-Ann Jones | Mary-Ann Jones | pass |
| NA | NA | NA | pass |
| pass | |||
| FAIL |
This collapses runs, but a single leading or trailing space survives (John Smith), and a blank string becomes a single space.
Attempt C: sentence case.
check_fn(function(x) x |> str_squish() |> str_to_sentence(), x11, clean_expected)| input | expected | got | verdict |
|---|---|---|---|
| jOHN sMITH | John Smith | John smith | FAIL |
| mary-ann jones | Mary-Ann Jones | Mary-ann jones | FAIL |
| NA | NA | NA | pass |
| pass | |||
| pass |
str_to_sentence() capitalizes only the first letter of the string. Pick the case function that matches the rule.
3.5.2 Challenge 25: Building a sentence from a vector with gaps
Task. Turn the vector into exactly this sentence, skipping missing values: Hello, Ann, Bob and Cy!
nm <- c("Ann", "Bob", NA, "Cy")
greet_expected <- "Hello, Ann, Bob and Cy!"Your turn
my_answer <- function(x) "" # write your answer here
check_value(my_answer(nm), greet_expected)greet <- function(x) {
str_c("Hello, ", str_flatten_comma(x, last = " and ", na.rm = TRUE), "!")
}
check_value(greet(nm), greet_expected)| expected | got | verdict |
|---|---|---|
| “Hello, Ann, Bob and Cy!” | “Hello, Ann, Bob and Cy!” | pass |
str_flatten_comma() collapses a vector into one string, with last replacing the final separator and na.rm = TRUE dropping the NA. str_c() then glues the pieces. str_glue() is the template-style alternative:
str_glue("Hello, {str_flatten_comma(nm, last = ' and ', na.rm = TRUE)}!")Hello, Ann, Bob and Cy!
Attempt A: base paste().
check_value(paste("Hello,", paste(nm, collapse = ", "), "!"), greet_expected)| expected | got | verdict |
|---|---|---|
| “Hello, Ann, Bob and Cy!” | “Hello, Ann, Bob, NA, Cy !” | FAIL |
paste() converts NA to the text "NA", puts a space before the ! (its default separator), and has no way to write “and”.
Attempt B: str_c(collapse = ).
check_value(str_c("Hello, ", str_c(nm, collapse = ", "), "!"), greet_expected)| expected | got | verdict |
|---|---|---|
| “Hello, Ann, Bob and Cy!” | NA_character_ | FAIL |
In stringr, NA is contagious: one missing value makes the whole result NA, with no warning.
Attempt C: str_flatten() with na.rm.
check_value(str_c("Hello, ", str_flatten(nm, ", ", na.rm = TRUE), "!"), greet_expected)| expected | got | verdict |
|---|---|---|
| “Hello, Ann, Bob and Cy!” | “Hello, Ann, Bob, Cy!” | FAIL |
The missing value is handled, but there is no “and”. Without na.rm = TRUE, str_flatten() returns NA for the whole thing, the same contagion as above.
3.5.3 Challenge 26: Sorting names with numbers
Task. Sort the file names so that file2.txt comes before file10.txt, the way a person would sort them.
fl <- c("file10.txt", "file2.txt", "file1.txt", "file20.txt")
sort_expected <- c("file1.txt", "file2.txt", "file10.txt", "file20.txt")Your turn
my_answer <- function(x) sort(x) # change this
check_value(my_answer(fl), sort_expected)check_value(str_sort(fl, numeric = TRUE), sort_expected)| expected | got | verdict |
|---|---|---|
| c(“file1.txt”, “file2.txt”, “file10.txt”, “file20.txt”) | c(“file1.txt”, “file2.txt”, “file10.txt”, “file20.txt”) | pass |
numeric = TRUE compares runs of digits as numbers. To reorder a whole data frame by a text column, use str_order(), which returns positions:
files <- data.frame(name = fl, size_kb = c(10, 2, 1, 20))
files[str_order(files$name, numeric = TRUE), ] name size_kb
3 file1.txt 1
2 file2.txt 2
1 file10.txt 10
4 file20.txt 20
Attempt A: base sort().
check_value(sort(fl), sort_expected)| expected | got | verdict |
|---|---|---|
| c(“file1.txt”, “file2.txt”, “file10.txt”, “file20.txt”) | c(“file1.txt”, “file10.txt”, “file2.txt”, “file20.txt”) | FAIL |
Text is compared character by character, so "1" in file10 is compared with "2" in file2, and file10 wins.
Attempt B: str_sort() without numeric.
check_value(str_sort(fl), sort_expected)| expected | got | verdict |
|---|---|---|
| c(“file1.txt”, “file2.txt”, “file10.txt”, “file20.txt”) | c(“file1.txt”, “file10.txt”, “file2.txt”, “file20.txt”) | FAIL |
str_sort() is locale-aware (useful for accents and case), but it still compares text. The numeric behavior must be requested.
3.6 The general rules for choosing a function
Ask two questions, in this order.
3.6.1 What do I want back?
| I want… | Use |
|---|---|
| a yes/no per string | str_detect() (or str_starts() / str_ends()) |
| the strings that match | str_subset() |
| their positions in the vector | str_which() |
| how many matches | str_count() |
| the matched text | str_extract() / str_extract_all() |
| the matched text split into groups | str_match() / str_match_all() |
| where the match sits | str_locate() / str_locate_all() |
| the text changed | str_replace() / str_replace_all() / str_remove() |
| pieces of the text | str_split() family, or str_sub() by position |
First match or every match? Add _all for every match. Remember: extract_all, match_all, locate_all, and split return lists; count, replace_all, and remove_all return vectors.
3.6.2 How should the pattern be read?
| If your pattern is… | Then |
|---|---|
plain text, possibly with . ( + * ? |
fixed("...") (or str_escape()) |
| a structure (“digits, then a dash, …”) | a regex, anchored if it is validation |
| case-insensitive | regex(..., ignore_case = TRUE) or (?i) for the whole pattern, (?i:...) for part of it |
| over multiple lines | regex(..., multiline = TRUE) for ^ / $, dotall = TRUE for . |
| long and complicated | regex(..., comments = TRUE) with comments |
| about words, sentences, or characters as people see them | boundary("word" / "sentence" / "character") |
| a language-aware comparison | coll() |
3.6.3 Rules of the road
Vectors in, vectors out. Functions are vectorized over the strings, and the pattern may be a vector too, in which case it is matched element by element:
str_detect(c("apple", "bread", "cake"), c("^a", "^b", "^a"))[1] TRUE TRUE FALSENAin,NAout. Missing values propagate, and a singleNAcan turn a wholestr_c()intoNA. Handle it deliberately withna.rm = TRUE,str_replace_na(), oris.na().Mind the return type.
str_extract_all()gives a list;str_match()gives a matrix whose first column is the whole match. Do not use the result of a_allfunction as if it were a plain vector.Escaping happens twice. R reads the string first, then the regex engine reads the pattern. To match a literal dot, write
"\\.". For a literal backslash in a replacement, write"\\\\".Replacements are templates.
\\1is a group, and a function gives per-match logic. A named vector of replacements runs sequentially.Combining is strict.
str_c()recycles only length-1 inputs and returnscharacter(0)if any input has length zero:tryCatch(str_c(c("a", "b"), c("x", "y", "z", "w")), error = function(e) cat(conditionMessage(e)))Can't recycle `..1` (size 2) to match `..2` (size 4).str_c("x", character(0))character(0)Build, then test small. Start with a tiny vector that has a normal case, an edge case, an empty string, and an
NA. Addstr_view()when a pattern surprises you.
3.7 Pipeline pitfall checklist
Before trusting a stringr expression, run through this list.
- Is the dot literal or a wildcard?
.matches any character. For literal text, usefixed()or\\.. - Did you escape for both layers?
"\\d", not"\d". A literal backslash needs"\\\\". - First match or every match?
str_replace()andstr_extract()stop at the first. - Is the pattern greedy?
.*runs to the last possible end. Use.*?or a negated class like[^;]. - Anchors:
^and$mean the whole string unlessmultiline = TRUE;.ignores newlines unlessdotall = TRUE. - Validation needs anchors. Without
^and$, a pattern accepts any string that merely contains a match. - Is the replacement syntax right?
\\1, not$1. No groups means no\\1. - Does one replacement see another’s output? Sequential replacements can undo or distort each other; use a function for simultaneous changes.
- What about
NA, empty strings, and extra whitespace? Put at least one of each in your test vector. - Does your class assume too much?
\w,[A-Za-z], and\deach make assumptions about letters, accents, and digits. Test with the awkward cases (o'clock,well-known,5). - Locale and case:
str_sort()andstr_to_title()follow language rules; basesort()may not.
3.8 Extra practice: pipelines
Short problems with the solution hidden. As before, write your answer first.
3.8.1 Expanding abbreviations
Replace the abbreviations St and Ave with Street and Avenue, but only when they are whole words.
st <- c("12 Stanley St", "5 Ave Q, Stone St", "Main Ave")
st_expected <- c("12 Stanley Street", "5 Avenue Q, Stone Street", "Main Avenue")abbrev <- c("\\bSt\\b" = "Street", "\\bAve\\b" = "Avenue")
check_fn(function(x) str_replace_all(x, abbrev), st, st_expected)| input | expected | got | verdict |
|---|---|---|---|
| 12 Stanley St | 12 Stanley Street | 12 Stanley Street | pass |
| 5 Ave Q, Stone St | 5 Avenue Q, Stone Street | 5 Avenue Q, Stone Street | pass |
| Main Ave | Main Avenue | Main Avenue | pass |
A named vector is a convenient way to apply several replacements in one call. The \\b word boundaries stop St from matching inside Stanley or Stone:
check_fn(function(x) str_replace_all(x, c("St" = "Street", "Ave" = "Avenue")), st, st_expected)| input | expected | got | verdict |
|---|---|---|---|
| 12 Stanley St | 12 Stanley Street | 12 Streetanley Street | FAIL |
| 5 Ave Q, Stone St | 5 Avenue Q, Stone Street | 5 Avenue Q, Streetone Street | FAIL |
| Main Ave | Main Avenue | Main Avenue | pass |
3.8.2 Overlapping matches
Count occurrences of aa including overlaps: aaaa contains three.
ov <- c("aaaa", "aaa", "a")
ov_expected <- c(3, 2, 0)check_count("(?=aa)", ov, ov_expected)| input | expected | got | verdict |
|---|---|---|---|
| aaaa | 3 | 3 | pass |
| aaa | 2 | 2 | pass |
| a | 0 | 0 | pass |
Normal matching consumes characters, so aa matches cannot overlap. A lookahead matches a position without consuming anything, so each position that starts aa is counted (the same zero-width idea as Part I). For comparison, the plain pattern gives:
check_count("aa", ov, ov_expected)| input | expected | got | verdict |
|---|---|---|---|
| aaaa | 3 | 2 | FAIL |
| aaa | 2 | 1 | FAIL |
| a | 0 | 0 | pass |
3.8.3 Extracting the middle piece
Extract the month from each ISO date, as text.
dates <- c("2024-05-17", "1999-12-31")
month_expected <- c("05", "12")check_fn(function(x) str_split_i(x, "-", 2), dates, month_expected)| input | expected | got | verdict |
|---|---|---|---|
| 2024-05-17 | 05 | 05 | pass |
| 1999-12-31 | 12 | 12 | pass |
check_fn(function(x) str_sub(x, 6, 7), dates, month_expected)| input | expected | got | verdict |
|---|---|---|---|
| 2024-05-17 | 05 | 05 | pass |
| 1999-12-31 | 12 | 12 | pass |
str_split_i() picks one piece by position after splitting. str_sub() slices by character position, which is faster but only safe when the format is fixed-width.
Write your own
Pick a text-cleaning job from your own data (names, IDs, addresses, survey answers). Decide which family it belongs to using the decision tables, write the pipeline, and create a test vector containing at least one NA, one empty string, and one input that a near-miss pattern would pass. Finding that input is the real skill.
3.9 Functions covered
| Function | Where it appears |
|---|---|
str_detect(), str_starts(), str_ends(), str_which(), str_subset() |
tour, Challenges 14, 15, 17 |
str_count() |
tour, Challenge 18, Extra practice |
str_locate() |
tour |
str_extract(), str_extract_all() |
tour, Challenges 15, 16 |
str_match(), str_match_all() |
Challenges 16, 19 |
str_replace(), str_replace_all(), str_remove() |
tour, Challenges 21, 22, 23, Extra practice |
str_split() family (str_split_1(), str_split_i(), str_split_fixed()) |
Challenges 15, 20, Extra practice |
str_sub(), str_pad(), str_trunc(), str_length() |
tour, Challenge 23 |
str_trim(), str_squish(), case functions |
tour, Challenge 24 |
str_c(), str_flatten(), str_flatten_comma(), str_glue(), str_replace_na() |
tour, Challenge 25 |
str_sort(), str_order() |
Challenge 26 |
fixed(), regex() modifiers, boundary(), str_escape(), str_view() |
tour, Challenges 14, 15, 17, 18 |