CMPSC 311: Introduction to Systems Programming Page 1
Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA
Systems and Internet Infrastructure Security
i i
Regular Expressions Devin J. Pohly <djpohly@cse.psu.edu> - - PowerPoint PPT Presentation
Systems and Internet i Infrastructure Security i Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA Regular Expressions Devin J. Pohly
CMPSC 311: Introduction to Systems Programming Page 1
Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA
i i
Page 2 CMPSC 311: Introduction to Systems Programming
replace tool
in shell scripts, as a text editor feature, or as part
Page 3 CMPSC 311: Introduction to Systems Programming
pattern
and X” for any X, Y
Page 4 CMPSC 311: Introduction to Systems Programming
Page 5 CMPSC 311: Introduction to Systems Programming
middle stands for
which looks for literal (“fixed”) strings
expressions
languages’ flavors
Page 6 CMPSC 311: Introduction to Systems Programming
egrep regex file(s)
egrep regex
expression, and file(s) are the files to search
Page 7 CMPSC 311: Introduction to Systems Programming
Page 8 CMPSC 311: Introduction to Systems Programming
match “fgh”
match “lmn”
the whole string
Page 9 CMPSC 311: Introduction to Systems Programming
characters from the shell!
Page 10 CMPSC 311: Introduction to Systems Programming
where the second, fourth, and sixth letters are “o”
at least 22 characters
Page 11 CMPSC 311: Introduction to Systems Programming
next slide
contain a, e, i, o, u in that
between)
Page 12 CMPSC 311: Introduction to Systems Programming
the previous thing to match?
that order and at least one letter in between each
Page 13 CMPSC 311: Introduction to Systems Programming
an empty argument
Page 14 CMPSC 311: Introduction to Systems Programming
match?
character classes
◾ Find words starting with b
where all the rest of the letters are s, a, or n
◾ Find all the words you can
type with only ASDFJKL
◾ Find all the words you can
type with AOEUHTNS!
Page 15 CMPSC 311: Introduction to Systems Programming
[b-df-hj-np-tv-z]
make with A through E
◾ … that are at least 5
letters long (hint: pipe the
Page 16 CMPSC 311: Introduction to Systems Programming
◾ Not quite – why?
q, followed by something
ranges
◾ Any character that isn’t a
digit: [^0-9]
Page 17 CMPSC 311: Introduction to Systems Programming
the entire group
followed by “ach” one or more times, followed by e
with the first, is an e
Page 18 CMPSC 311: Introduction to Systems Programming
Page 19 CMPSC 311: Introduction to Systems Programming
search your code for the expression ‘int *\*’ to find integer pointers
between the two *s?
Page 20 CMPSC 311: Introduction to Systems Programming
same characters again
from the start of the regex
four-character sequence repeated immediately
Page 21 CMPSC 311: Introduction to Systems Programming
$ sed -r 's/([a-z]+) and ([a-z]+)/\2 and \1/g'
Page 22 CMPSC 311: Introduction to Systems Programming
your regex-fu
tutorial puzzles and works up