Regex Flashcards
Regular expressions
A language independent approach to expressing patterns
Hierarchy of languages
Type 0: unrestricted grammars
Type 1: context-sensitive grammars
Type 2: context-free grammars
Type 3: regular grammars
Type 2 used in parsers
Type 3 used in regular expressions and lexical analyzers
Grep
Global regular expression print
- arg 1: a regex
- arg 2: a set of files where grep will try to find strings matching regex
Start with
[^]
Not in set
.
Any char
-w
Contains that specific word
-i
Case insensitive
$
Ends with
a*
Zero or more reps of ‘a’
a+
one or more reps of ‘a’
a?
Zero or one rep of ‘a’
a{5}
Exactly 5 reps of ‘a’
a{3,7}
3 to 7 reps of ‘a’
[abc]
Any one char in the set
a|b
Match ‘a’ or ‘b’
()
Group a component of symbols in the regex
\
Escape any meta symbol
\d
Any decimal digit
\w
Any alphanumeric char
\s
Any white space char
\b
Empty string at a word boundary
\D
Match any non-digit char
\W
Match any non-alphanumeric char
\S
Match any non-white spice char
\B
Empty string not at a word boundary
\number
Matches text of group number
re.match()
(string, pattern)
Return address
re.compile()
Can use to reuse patterns
Helps to gain speed
Word boundary
The first letter that starts a given word
re.search()
Provides us with index (gives first index of first match
- returns none if not found
- but r in front to look at the raw text