SlideShare uma empresa Scribd logo
1 de 20
Regular Expressions
Boot Camp
Presented by Chris Schiffhauer
www.schiffhauer.com
twitter.com/PaulyGlott
What are Regular Expressions?
• Regular expressions are an extension of wildcards (i.e. *.doc).
• Code that manipulates text needs to locate strings that match
complex patterns.
• A regular expression is a shorthand for a pattern.
• w+ is a concise way to say “match any non-null strings of alphanumeric
characters.
Finding Nemo
• nemo

Find nemo

• When ignoring case, will match “Nemo”, “NEMO”, or “nEmO”.
• Will also match characters 9-12 of “Johnny Mnemonic”, or “Finding Nemo 2”.

• bnemob

Find nemo as a whole word

• b is a code that says “match the position at the beginning of end of any word”.
• Will only match complete words spelled “nemo” with any combination of upper and
lowercase letters.

• bnemob.*b2b Find text with “nemo” followed by “2”
• The special characters that give Regular Expressions their power is already making
them hard for humans to read.
Determining the Validity of Phone
Numbers
• bddd-ddd-dddd
• d
•-

Matches any single digit.
Literal hyphen (has no special meaning).

• bd{3}-d{3}-d{4}
• {3}

Find ten-digit US phone number

Better way to find the number

Follows d to mean “repeat the preceding character three times”.
Special Characters
• baw*b Find words that start with the letter a
•
•
•
•

b
a
w*
b

• d+
•+

The beginning of a word.
The letter “a”.
Any number of repetitions of alphanumeric characters.
The end of a word.

Find repeated strings of digits
Similar to *, but requires one repetition.
Special Characters, continued
• bw{6}b Find six letter words
•
•
•
•
•
•
•

.
w
s
d
b
^
$

Match any character except newline
Match any alphanumeric character
Match any whitespace character
Match any digit
Match the beginning or end of a word
Match the beginning of the string
Match the end of the string
Beginnings and Endings
• ^d{3}-d{3}-d{4}$

Validate an entire string as a phone number

•^
The beginning of the string.
•$
The end of the string.
• In .NET, use RegexOptions.Multiline to match the beginning and end of a line.

• ^$1000$
•
•
•
•

^
$
1000
$

Find “$1000” as the entire string
The beginning of the string.
Escaped “$” (literal “$”).
Literal “1000”.
The end of the string.
Wash, Rinse, Repeat
•*
•+
•?
• {n}
• {n,m}
• {n,}

Repeat any number of times
Repeat one or more times
Repeat zero or one time
Repeat n times
Repeat at least n, but not more than m times
Repeat at least n times.
Wash, Rinse, Repeat, continued
• bw{5,6}b
• w{5,6}

Find all five and six letter words
Word with at least 5, but not more than 6, characters.

• b+d{1,3}sd{3}-d{3}-d{4}
Find phone numbers formatted for int’l calling
• s

White space

• d{3}-d{2}-d{4} Find social security numbers
• ^w*
Find first word in string
Character Classes
• [aeiou]

Matches any vowel

• [.?!]

Matches punctuation at the end of a sentence

•.
•?

Literal “.”, losing its special meaning because it’s inside brackets
Literal “?”

• (?d{3}[) ]s?d{3}[ ]d{4}

Matches a 10-digit phone number

• (?
Zero or one left parentheses.
• [) ]
A right parenthesis or a space.
• Will also match “480) 555-1212”.
Negation
• W
• S
• D
• B
• [^x]
• [^aeiou]

Match any character that is NOT alphanumeric
Match any character that is NOT whitespace
Match any character that is NOT a digit
Match a position that is NOT a word boundary
Match any character that is NOT “x”
Match any character that is NOT one of the chars “aeiou”

• S+

All strings that do not contain whitespace characters
Alternatives
•|

Pipe symbol separates alternatives

• bd{5}-d{4}b|bd{5}b

Five and nine digit Zip Codes

• bd{5}-d{4}b Leftmost alternative first: nine digit Zip Codes.
• bd{5}b
Second: five digit Zip Codes.

• bd{5}b|bd{5}-d{4}b

Only matches five digit Zip Codes

• ((d{3})|d{3})s?d{3}[- ]d{4}
• ((d{3})|d{3})

Ten digit phone numbers

Matches “(480)” or “480”.
Grouping
Parentheses delimit a subexpression to allow repetition or special
treatment.
• (d{1,3}.){3}d{1,3}

A simple IP address finder

• (d{1,3}.)
A one to three digit number following by a literal period.
• {3}
Repeats the preceding three times.
• Also matches invalid IP addresses like “999.999.999.999”.

• ((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)
A better IP address finder
Backreferences
Backreferences search for a recurrence of previously matched text that
has been captured by a group.
• b(w+)bs*1b
• (w+)
• s*
• 1

Find repeated words
Finds a string of at least one character within group 1.
Finds any amount of whitespace.
Finds a repetition of the captured text.
Backreferences, continued
Automatic numbering of groups can be overridden by specifying an
explicit name or number.
• b(?<Word>w+)bs*k<Word>b
Capture repeated word in a named group
• (?<Word>w+) Names this capture group “Word”.
Captures and Lookarounds
• Captures
• (exp)
Match “exp” & capture in an automatically numbered group.
• (?<name>exp) Match “exp” and capture it in a group named name.
• (?:exp)
Match “exp”, but do not capture it.

• Lookarounds
text
•
•
•
•

(?=exp)
(?<exp)
(?!exp)
(?<!exp)

Match a position like ^ or b and never match any
Match any position preceding a suffix “exp”.
Match any position following a prefix “exp”.
Match any position after which the suffix “exp” isn’t found.
Match any position before which the prefix “exp” isn’t found.
Positive Lookaround
• bw+(?=ingb)
• (?=ing)

The beginning of words ending with “ing”
“Zero-width positive lookahead assertion”
Matches a position that precedes a given suffix.

• (?<=bre)w+b

The end of words starting with “re”

• (?<=bre)

“Zero-width positive lookbehind assertion”
Matches a position following a prefix.

• (?<=d)d{3}b

3 digits at the end of a word, preceded by a digit

• (?<=s)w+(?=s) Alphanumeric strings bounded by whitespace
Negative Lookaround
• bw*q[^u]w*b
• [^u]

Always matches a character. “Iraq” does not match.

• bw*q(?!u)w*b
• (?!u)

Words with “q” followed by NOT “u”

Search for words with “q” not followed by “u”
“Zero-width negative lookahead assertion”
Succeeds when “u” does not exist. “Iraq” matches.

• (?<![a-z ])w{7} 7 alphanumerics not preceded by a letter or space
• (?<![a-z ])

“Zero-width negative lookbehind assertion”
Greedy and Lazy
Be default, regular expressions are “greedy”. This means that when a
quantifier can accept a range of repeitions, as many characters as
possible will be matched.
• a.*b
The longest string starting with “a” and ending with “b”
• An input of “aabab” will match the entire string.

Quantifiers can be made lazy by adding a question mark.
• a.*?b
The shortest string starting with an a and ending with a b
• An input of “aabab” will match “aab” and then “ab”.
Greedy and Lazy, continued
• *?
• +?
• ??
• {n,m}?
• {n,}?

Repeat any number of times, but as few as possible.
Repeat one or more times, but as few as possible.
Repeat zero or one time, but as few as possible.
Repeat at least n, but no more than m, as few as possible.
Repeat at least n times, but as few as possible.

Mais conteúdo relacionado

Semelhante a Regular Expressions Boot Camp (20)

Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions using Python
Regular expressions using PythonRegular expressions using Python
Regular expressions using Python
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
An Introduction to Regular expressions
An Introduction to Regular expressionsAn Introduction to Regular expressions
An Introduction to Regular expressions
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Regular expressions-ada-2018
Regular expressions-ada-2018Regular expressions-ada-2018
Regular expressions-ada-2018
 
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in PracticeWeek-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
 
test vedio
test vediotest vedio
test vedio
 
qwdeqwe
qwdeqweqwdeqwe
qwdeqwe
 
Added to test pdf
Added to test pdf Added to test pdf
Added to test pdf
 
added for test
added for test added for test
added for test
 
ganesh testing
ganesh testing ganesh testing
ganesh testing
 
now its pdf
now its pdfnow its pdf
now its pdf
 
fghfghf
fghfghffghfghf
fghfghf
 
The hindu
The hinduThe hindu
The hindu
 
Video added by Normal user
Video added by Normal user Video added by Normal user
Video added by Normal user
 
Added to test pdf
Added to test pdf Added to test pdf
Added to test pdf
 
dasdasd
dasdasddasdasd
dasdasd
 
estset
estsetestset
estset
 

Último

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Último (20)

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 

Regular Expressions Boot Camp

  • 1. Regular Expressions Boot Camp Presented by Chris Schiffhauer www.schiffhauer.com twitter.com/PaulyGlott
  • 2. What are Regular Expressions? • Regular expressions are an extension of wildcards (i.e. *.doc). • Code that manipulates text needs to locate strings that match complex patterns. • A regular expression is a shorthand for a pattern. • w+ is a concise way to say “match any non-null strings of alphanumeric characters.
  • 3. Finding Nemo • nemo Find nemo • When ignoring case, will match “Nemo”, “NEMO”, or “nEmO”. • Will also match characters 9-12 of “Johnny Mnemonic”, or “Finding Nemo 2”. • bnemob Find nemo as a whole word • b is a code that says “match the position at the beginning of end of any word”. • Will only match complete words spelled “nemo” with any combination of upper and lowercase letters. • bnemob.*b2b Find text with “nemo” followed by “2” • The special characters that give Regular Expressions their power is already making them hard for humans to read.
  • 4. Determining the Validity of Phone Numbers • bddd-ddd-dddd • d •- Matches any single digit. Literal hyphen (has no special meaning). • bd{3}-d{3}-d{4} • {3} Find ten-digit US phone number Better way to find the number Follows d to mean “repeat the preceding character three times”.
  • 5. Special Characters • baw*b Find words that start with the letter a • • • • b a w* b • d+ •+ The beginning of a word. The letter “a”. Any number of repetitions of alphanumeric characters. The end of a word. Find repeated strings of digits Similar to *, but requires one repetition.
  • 6. Special Characters, continued • bw{6}b Find six letter words • • • • • • • . w s d b ^ $ Match any character except newline Match any alphanumeric character Match any whitespace character Match any digit Match the beginning or end of a word Match the beginning of the string Match the end of the string
  • 7. Beginnings and Endings • ^d{3}-d{3}-d{4}$ Validate an entire string as a phone number •^ The beginning of the string. •$ The end of the string. • In .NET, use RegexOptions.Multiline to match the beginning and end of a line. • ^$1000$ • • • • ^ $ 1000 $ Find “$1000” as the entire string The beginning of the string. Escaped “$” (literal “$”). Literal “1000”. The end of the string.
  • 8. Wash, Rinse, Repeat •* •+ •? • {n} • {n,m} • {n,} Repeat any number of times Repeat one or more times Repeat zero or one time Repeat n times Repeat at least n, but not more than m times Repeat at least n times.
  • 9. Wash, Rinse, Repeat, continued • bw{5,6}b • w{5,6} Find all five and six letter words Word with at least 5, but not more than 6, characters. • b+d{1,3}sd{3}-d{3}-d{4} Find phone numbers formatted for int’l calling • s White space • d{3}-d{2}-d{4} Find social security numbers • ^w* Find first word in string
  • 10. Character Classes • [aeiou] Matches any vowel • [.?!] Matches punctuation at the end of a sentence •. •? Literal “.”, losing its special meaning because it’s inside brackets Literal “?” • (?d{3}[) ]s?d{3}[ ]d{4} Matches a 10-digit phone number • (? Zero or one left parentheses. • [) ] A right parenthesis or a space. • Will also match “480) 555-1212”.
  • 11. Negation • W • S • D • B • [^x] • [^aeiou] Match any character that is NOT alphanumeric Match any character that is NOT whitespace Match any character that is NOT a digit Match a position that is NOT a word boundary Match any character that is NOT “x” Match any character that is NOT one of the chars “aeiou” • S+ All strings that do not contain whitespace characters
  • 12. Alternatives •| Pipe symbol separates alternatives • bd{5}-d{4}b|bd{5}b Five and nine digit Zip Codes • bd{5}-d{4}b Leftmost alternative first: nine digit Zip Codes. • bd{5}b Second: five digit Zip Codes. • bd{5}b|bd{5}-d{4}b Only matches five digit Zip Codes • ((d{3})|d{3})s?d{3}[- ]d{4} • ((d{3})|d{3}) Ten digit phone numbers Matches “(480)” or “480”.
  • 13. Grouping Parentheses delimit a subexpression to allow repetition or special treatment. • (d{1,3}.){3}d{1,3} A simple IP address finder • (d{1,3}.) A one to three digit number following by a literal period. • {3} Repeats the preceding three times. • Also matches invalid IP addresses like “999.999.999.999”. • ((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?) A better IP address finder
  • 14. Backreferences Backreferences search for a recurrence of previously matched text that has been captured by a group. • b(w+)bs*1b • (w+) • s* • 1 Find repeated words Finds a string of at least one character within group 1. Finds any amount of whitespace. Finds a repetition of the captured text.
  • 15. Backreferences, continued Automatic numbering of groups can be overridden by specifying an explicit name or number. • b(?<Word>w+)bs*k<Word>b Capture repeated word in a named group • (?<Word>w+) Names this capture group “Word”.
  • 16. Captures and Lookarounds • Captures • (exp) Match “exp” & capture in an automatically numbered group. • (?<name>exp) Match “exp” and capture it in a group named name. • (?:exp) Match “exp”, but do not capture it. • Lookarounds text • • • • (?=exp) (?<exp) (?!exp) (?<!exp) Match a position like ^ or b and never match any Match any position preceding a suffix “exp”. Match any position following a prefix “exp”. Match any position after which the suffix “exp” isn’t found. Match any position before which the prefix “exp” isn’t found.
  • 17. Positive Lookaround • bw+(?=ingb) • (?=ing) The beginning of words ending with “ing” “Zero-width positive lookahead assertion” Matches a position that precedes a given suffix. • (?<=bre)w+b The end of words starting with “re” • (?<=bre) “Zero-width positive lookbehind assertion” Matches a position following a prefix. • (?<=d)d{3}b 3 digits at the end of a word, preceded by a digit • (?<=s)w+(?=s) Alphanumeric strings bounded by whitespace
  • 18. Negative Lookaround • bw*q[^u]w*b • [^u] Always matches a character. “Iraq” does not match. • bw*q(?!u)w*b • (?!u) Words with “q” followed by NOT “u” Search for words with “q” not followed by “u” “Zero-width negative lookahead assertion” Succeeds when “u” does not exist. “Iraq” matches. • (?<![a-z ])w{7} 7 alphanumerics not preceded by a letter or space • (?<![a-z ]) “Zero-width negative lookbehind assertion”
  • 19. Greedy and Lazy Be default, regular expressions are “greedy”. This means that when a quantifier can accept a range of repeitions, as many characters as possible will be matched. • a.*b The longest string starting with “a” and ending with “b” • An input of “aabab” will match the entire string. Quantifiers can be made lazy by adding a question mark. • a.*?b The shortest string starting with an a and ending with a b • An input of “aabab” will match “aab” and then “ab”.
  • 20. Greedy and Lazy, continued • *? • +? • ?? • {n,m}? • {n,}? Repeat any number of times, but as few as possible. Repeat one or more times, but as few as possible. Repeat zero or one time, but as few as possible. Repeat at least n, but no more than m, as few as possible. Repeat at least n times, but as few as possible.