Uploaded by fsqcds

regex-mictoteaching

advertisement
Introduction to Regular
Expressions (Regex)
What can we do with regex?
•
Find patterns in text (like addresses)
•
Check for matching to format (like checking user
input for email).
•
Replacing.
Where can we use regex?
•
Almost every programming language.
•
Unix tools like grep, sed, awk.
•
Text editors like Vim, Emacs, Sublime Text and
most of IDEs.
Matching string literals
Pattern: /foo/
Text: foobar foobaz
The Dot
Dot (.) matches any character.
Pattern: /.ab./
Text: abcaabbxyz12ab34
Character Classes
[abc]
a, b, or c
[^abc]
Any character except a, b, or c
[a-zA-Z]
a through z, or A through Z
Example:
/[bcr]at/ will match “bat”, but will not match “hat”.
Quantifiers
Quantifiers allow you to specify the number of
occurrences to match against.
X?
X, once or not at all
X*
X, zero or more times
X+
X, one or more times
X{n}
X, exactly n times
X{n,}
X, at least n times
X{n,m}
X, at least n but not more than m times
Resources
•
regexone.com: Learn Regular Expressions with
simple, interactive exercises.
•
regexr.com: A website for interactive regex
prototyping with syntax highlighting.
•
regexcrossword.com: A puzzle game, where the
crossword clues are defined using regular
expressions.
•
play.inginf.units.it: Are you a regex ninja? Prove it.
Download