For these problems, use your plain text editor (BBedit, Notepad++, or something else) to type in the problem text and use the search function to write a regular expression that gives the desired result. In your homework solution, provide the regular expression that works (there are several ways to solve each problem) within some plain text fencing on your markdown page, and add a bit of markdown text to explain what each element of your regular expression is doing. If you get stuck, give the solution that gets you as close as you can.
First String Second 1.22 3.4
Second More Text 1.555555 2.2220
Third x 3 124
Don’t worry about how many tab spaces are needed to set this up, just
make sure the columns are aligned. Now, using a single regular
expression, transform these lines into what we need for a proper
.csv
file:
First String,Second,1.22,3.4
Second,More Text,1.555555,2.2220
Third,x,3,124
Here are a few lines of my conflict list:
Ballif, Bryan, University of Vermont
Ellison, Aaron, Harvard Forest
Record, Sydne, Bryn Mawr
However, my collaborator asked me to please provide to her the list in this format:
Bryan Ballif (University of Vermont)
Aaron Ellison (Harvard Forest)
Sydne Record (Bryn Mawr)
Write a single regular expression that will make the change.
The list of tunes (shown here as a single line of text) looks like this:
0001 Georgia Horseshoe.mp3 0002 Billy In The Lowground.mp3 0003 Winder Slide.mp3 0004 Walking Cane.mp3
Unfortunately, in this form, you can’t re-order the file names to put them in alphabetical order. I thought I could just strip out the leading numbers, but this will cause a conflict, because, for wildly popular tunes such as “Shove That Pig’s Foot A Little Further In The Fire”, there are multiple copies somewhere in the list.
All of these files are on a single line, so first write a regular expression to place each file name on its own line:
0001 Georgia Horseshoe.mp3
0002 Billy In The Lowground.mp3
0003 Winder Slide.mp3
0004 Walking Cane.mp3
Georgia Horseshoe_0001.mp3
Billy In The Lowground_0002.mp3
Winder Slide_0003.mp3
Walking Cane_0004.mp3
Camponotus,pennsylvanicus,10.2,44
Camponotus,herculeanus,10.5,3
Myrmica,punctiventris,12.2,4
Lasius,neoniger,3.3,55
Write a single regular expression to rearrange the data set like this:
C_pennsylvanicus,44
C_herculeanus,3
M_punctiventris,4
L_neoniger,55
C_penn,44
C_herc,3
M_punc,4
L_neon,55
Campen, 44, 10.2
Camher, 3, 10.5
Myrpun, 4, 12.2
Lasneo, 55, 3.3
Check out these additional sources for more information on using regular expressions:
Regular Expressions Tutorial.pdf
For a tutorial on using regular expressions in R
using the package stringr:
stringr Tutorial