Working with Strings

Stat 251

2025-03-25

Working with regular expressions

str_view in stringr package (in R) shows all matches:

stringr::str_view("> Poem for a Friend. ", pattern = "\\W")
[1] │ <>>< >Poem< >for< >a< >Friend<.>< >
stringr::str_view("> Poem for a Friend. ", pattern = "\\b")
[1] │ > <>Poem<> <>for<> <>a<> <>Friend<>. 

str_count counts all matches in R:

stringr::str_count("> Poem for a Friend. ", pattern = "\\b")/2
[1] 4

find_all and len count all matches in python:

import re
res = re.findall("\\b", "> Poem for a Friend. ")
len(res)/2
4.0

Logistics

  • this week: reading quizzes (just submitted)

  • work on homework #7: string operations using reddit poetry :)

  • submit any changes to homework #1 - #5 by Thursday

Homework 7

Reddit poets: sprog and schnoodledoodledoo

Full points for completion in ONE language

An Example

Reddit OneOrangeBraincell

In the comments:

Comment
byu/Falom from discussion
inOneOrangeBraincell

Major String Operations

Operation Sketch
Find and replace (or remove) A row of three boxes. In the next row, the middle box is replaced with a different, slightly larger box.
Split data into multiple columns A column consisting of multiple rows of three boxes. An arrow indicates a transition from the single column to multiple columns, each containing only one type of box.
Join data into a single column A set of three columns, each containing one type of box. An arrow indicates a transition whereby in each row, the three columns of data are combined into a single column.