# A tibble: 980 × 4
word i vowels consonants
<chr> <int> <int> <int>
1 a 1 1 0
2 able 2 2 2
3 about 3 3 2
4 absolute 4 4 4
5 accept 5 2 4
6 account 6 3 4
7 achieve 7 4 3
8 across 8 2 4
9 act 9 1 2
10 active 10 3 3
# … with 970 more rows
1.12 Extract matches
sentences is a collection of 720 phrases:
length(sentences)
[1] 720
head(sentences)
[1] "The birch canoe slid on the smooth planks."
[2] "Glue the sheet to the dark blue background."
[3] "It's easy to tell the depth of a well."
[4] "These days a chicken leg is a rare dish."
[5] "Rice is often served in round bowls."
[6] "The juice of lemons makes fine punch."
Suppose we want to find all sentences that contain a colour.
[1] "Glue the sheet to the dark blue background."
[2] "Two blue fish swam in the tank."
[3] "The colt reared and threw the tall rider."
[4] "The wide road shimmered in the hot sun."
[5] "See the cat glaring at the scared mouse."
[6] "A wisp of cloud hung in the blue air."
[7] "Leaves turn brown and yellow in the fall."
[8] "He ordered peach pie with ice cream."
[9] "Pure bred poodles have curls."
[10] "The spot on the blotter was made by green ink."
[11] "Mud was spattered on the front of his white shirt."
[12] "The sofa cushion is red and of light weight."
[13] "The sky that morning was clear and bright blue."
[14] "Torn scraps littered the stone floor."
[15] "The doctor cured him with these pills."
[16] "The new girl was fired today at noon."
[17] "The third act was dull and tired the players."
[18] "A blue crane is a tall wading bird."
[19] "Live wires should be kept covered."
[20] "It is hard to erase blue or red ink."
[21] "The wreck occurred by the bank on Main Street."
[22] "The lamp shone with a steady green flame."
[23] "The box is held by a bright red snapper."
[24] "The prince ordered his head chopped off."
[25] "The houses are built of red clay bricks."
[26] "The red tape bound the smuggled food."
[27] "Nine men were hired to dig the ruins."
[28] "The flint sputtered and lit a pine torch."
[29] "Hedge apples may stain your hands green."
[30] "The old pan was covered with hard fudge."
[31] "The plant grew large and green in the window."
[32] "The store walls were lined with colored frocks."
[33] "The purple tie was ten years old."
[34] "Bathe and relax in the cool green grass."
[35] "The clan gathered on each dull night."
[36] "The lake sparkled in the red hot sun."
[37] "Mark the spot with a sign painted red."
[38] "Smoke poured out of every crack."
[39] "Serve the hot rum to the tired heroes."
[40] "The couch cover and hall drapes were blue."
[41] "He offered proof in the form of a large chart."
[42] "A man in a blue sweater sat at the desk."
[43] "A sip of tea revives his tired friend."
[44] "The door was barred, locked, and bolted as well."
[45] "A thick coat of black paint covered all."
[46] "The small red neon lamp went out."
[47] "Paint the sockets in the wall dull green."
[48] "Wake and rise, and step into the green outdoors."
[49] "The green light in the brown box flickered."
[50] "He put his last cartridge into the gun and fired."
[51] "The ram scared the school children off."
[52] "Tear a thin sheet from the yellow pad."
[53] "Dimes showered down from all sides."
[54] "The sky in the west is tinged with orange red."
[55] "The red paper brightened the dim stage."
[56] "The hail pattered on the burnt brown grass."
[57] "The big red apple fell to the ground."
more <- sentences[str_count(sentences, colour_match) >1]str_view_all(more, colour_match)
[1] │ It is hard to erase <blue> or <red> ink.
[2] │ The <green> light in the brown box flicke<red>.
[3] │ The sky in the west is tinged with <orange> <red>.
x <-c("a", "a b", "a b c")str_extract_all(x, "[a-z]", simplify =TRUE)
[,1] [,2] [,3]
[1,] "a" "" ""
[2,] "a" "b" ""
[3,] "a" "b" "c"
1.13 Grouped matches
str_extract() gives us the complete match:
# why "([^ ]+)" match a word?noun <-"(a|the) ([^ ]+)"has_noun <- sentences %>%str_subset(noun) %>%head(10)has_noun %>%str_extract(noun)
[1] "the smooth" "the sheet" "the depth" "a chicken" "the parked"
[6] "the sun" "the huge" "the ball" "the woman" "a helps"
str_match() gives each individual component:
has_noun %>%str_match(noun)
[,1] [,2] [,3]
[1,] "the smooth" "the" "smooth"
[2,] "the sheet" "the" "sheet"
[3,] "the depth" "the" "depth"
[4,] "a chicken" "a" "chicken"
[5,] "the parked" "the" "parked"
[6,] "the sun" "the" "sun"
[7,] "the huge" "the" "huge"
[8,] "the ball" "the" "ball"
[9,] "the woman" "the" "woman"
[10,] "a helps" "a" "helps"
# A tibble: 720 × 3
sentence article noun
<chr> <chr> <chr>
1 The birch canoe slid on the smooth planks. the smooth
2 Glue the sheet to the dark blue background. the sheet
3 It's easy to tell the depth of a well. the depth
4 These days a chicken leg is a rare dish. a chicken
5 Rice is often served in round bowls. <NA> <NA>
6 The juice of lemons makes fine punch. <NA> <NA>
7 The box was thrown beside the parked truck. the parked
8 The hogs were fed chopped corn and garbage. <NA> <NA>
9 Four hours of steady work faced us. <NA> <NA>
10 A large size in stockings is hard to sell. <NA> <NA>
# … with 710 more rows
1.14 Replacing matches
Replace the first match:
x <-c("apple", "pear", "banana")str_replace(x, "[aeiou]", "-")
[1] "-pple" "p-ar" "b-nana"
Replace all matches:
str_replace_all(x, "[aeiou]", "-")
[1] "-ppl-" "p--r" "b-n-n-"
Multiple replacement:
x <-c("1 house", "2 cars", "3 people")str_replace_all(x, c("1"="one", "2"="two", "3"="three"))
[1] "one house" "two cars" "three people"
Back-reference:
# flip the order of the second and third wordssentences %>%str_replace("([^ ]+) ([^ ]+) ([^ ]+)", "\\1 \\3 \\2") %>%head(5)
[1] "The canoe birch slid on the smooth planks."
[2] "Glue sheet the to the dark blue background."
[3] "It's to easy tell the depth of a well."
[4] "These a days chicken leg is a rare dish."
[5] "Rice often is served in round bowls."