SlideShare a Scribd company logo
Regular
Expressions
Jesse Anderson
What Are They?
• Language to parse text
• Apply logic and constraints
• Concise (but not readable)
• Consistent (mostly)
• Widely supported in programming
languages
Hello Regex
Source Text Regular Expression Yield
“hello world” “hello” { “hello” }
“hello world hello world” “hello” { “hello”, “hello” }
“hello world hello world” “world” { “world”, “world” }
“hello world hello world” “hello world”
{ “hello world”, “hello
world” }
Java Regex Code
Pattern pattern = Pattern.compile("hello");
Matcher matcher = pattern.matcher("hello world");
// Find all matches
while (matcher.find()) {
// Get the matching string
String match = matcher.group();
// match = “hello”
}
C# Regex Code
foreach (Match match in
Regex.Matches("hello world", "hello",
RegexOptions.IgnoreCase)) {
// Get the matching string
String match = match.Value;
// match = “hello”
}
Python Regex Code
regex = re.compile("hello");
results = regex.search("hello world");
// results = "hello"
Perl Regex Code
$value = "hello world";
$value =~ m/hello/;
$result = $1;
// result = "hello"
The (Ugly) Alternative
String needle = "hello";
String haystack = "hello world hello world";
int index = 0;
while ((index = haystack.indexOf( needle,
index )) != -1) {
String match = haystack.substring( index,
index + needle.length() );
index++;
}
Regex Metacharacters
• * - Match zero or more times
• ? - Match zero or 1 time
• + - Match one or more times
• ^ - Match the start of a string
• $ - Match the end of a string
Character Classes
POSIXPOSIX ShorthandShorthand LonghandLonghand DescriptionDescription
[:word:] w [A-Za-z0-9_] Alphanumeric Chars.
W [^A-Za-z0-9_]
Non-alphanumeric
Chars.
[:alpha:] [A-Za-z] Alphabetic Chars.
[:blank:] [ t] Space and tab
[:digit:] d [0-9] Numeric Characters
D [^0-9] Non-numeric Chars.
[:space:] s [ trnvf] Whitespace Characters
Groups
Source TextSource Text Regular ExpressionRegular Expression YieldYield
“hello world” “([a-z]+)s+([a-z]+)
{ “hello world”,
“hello”, “world” }
“hello world12345” “([a-z]+)s+([a-z]+)
{ “hello world”,
“hello”, “world” }
“hello world12345” “([a-z]+)s+([a-z]+)(d+)
{ “hello
world12345”, “hello”,
“world”, “12345” }
Example
• Example that parses, cleans up, and
normalizes input
Recommended
Reading
• Mastering Regular Expressions by Jeffry
Friedl
• Regular Expressions Cheat Sheet
http://www.addedbytes.com/cheat-sheets/regular-e
• Regex Evaluator
http://www.cuneytyilmaz.com/prog/jrx/

More Related Content

PPT
Introduction to regular expressions
PPT
Textpad and Regular Expressions
PPTX
Regular Expressions 101 Introduction to Regular Expressions
KEY
Andrei's Regex Clinic
KEY
Regular Expressions 101
PPTX
Regular Expression
PPTX
Regular expressions
PDF
3.2 javascript regex
Introduction to regular expressions
Textpad and Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
Andrei's Regex Clinic
Regular Expressions 101
Regular Expression
Regular expressions
3.2 javascript regex

What's hot (20)

PPT
The Power of Regular Expression: use in notepad++
PPTX
Introduction to Regular Expressions
PPT
Regular Expressions
PPT
Regular expressions
PPTX
Bioinformatica p2-p3-introduction
PPT
Introduction to Regular Expressions RootsTech 2013
ODP
Regex Presentation
PPT
Regular Expression
ODP
Regular Expression
PPTX
Regular expressions
PPT
Regular Expressions grep and egrep
PPTX
Regular expression examples
PPTX
Finaal application on regular expression
PPT
Regular Expressions 2007
PPTX
Regular expression
PPTX
Regular Expression (Regex) Fundamentals
PPTX
Regex lecture
PPTX
Regular expressions
PDF
Regular expressions in Ruby and Introduction to Vim
PPT
Php String And Regular Expressions
The Power of Regular Expression: use in notepad++
Introduction to Regular Expressions
Regular Expressions
Regular expressions
Bioinformatica p2-p3-introduction
Introduction to Regular Expressions RootsTech 2013
Regex Presentation
Regular Expression
Regular Expression
Regular expressions
Regular Expressions grep and egrep
Regular expression examples
Finaal application on regular expression
Regular Expressions 2007
Regular expression
Regular Expression (Regex) Fundamentals
Regex lecture
Regular expressions
Regular expressions in Ruby and Introduction to Vim
Php String And Regular Expressions
Ad

Viewers also liked (6)

PDF
Regular language and Regular expression
PPTX
Regular expression (compiler)
PDF
Lecture: Regular Expressions and Regular Languages
PPT
Chapter 6 - Process Synchronization
PPTX
Process synchronization in Operating Systems
PDF
Memory management
Regular language and Regular expression
Regular expression (compiler)
Lecture: Regular Expressions and Regular Languages
Chapter 6 - Process Synchronization
Process synchronization in Operating Systems
Memory management
Ad

Similar to Introduction to Regular Expressions (20)

PDF
Regular expressions
PPTX
Python advanced 2. regular expression in python
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
PPTX
php string part 4
PPTX
Regular expressions
PPT
Class 5 - PHP Strings
PDF
Json the-x-in-ajax1588
PDF
Regex - Regular Expression Basics
ODP
Regular Expressions and You
PPTX
P3 2018 python_regexes
PDF
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
PPTX
Regular Expressions(Theory of programming languages))
PPTX
First steps in C-Shell
PDF
Regular expression for everyone
PPTX
Regular_Expressions.pptx
PPTX
Regular expressions
PPTX
P3 2017 python_regexes
PDF
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
PPTX
PHP Strings and Patterns
Regular expressions
Python advanced 2. regular expression in python
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
php string part 4
Regular expressions
Class 5 - PHP Strings
Json the-x-in-ajax1588
Regex - Regular Expression Basics
Regular Expressions and You
P3 2018 python_regexes
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
Regular Expressions(Theory of programming languages))
First steps in C-Shell
Regular expression for everyone
Regular_Expressions.pptx
Regular expressions
P3 2017 python_regexes
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
PHP Strings and Patterns

More from Jesse Anderson (13)

PDF
Managing Real-Time Data Teams
PDF
Pulsar for Kafka People
PDF
Big Data and Analytics in the COVID-19 Era
PDF
Working Together As Data Teams V1
PDF
What Does an Exec Need to About Architecture and Why
PDF
The Five Dysfunctions of a Data Engineering Team
PPTX
HBaseCon 2014-Just the Basics
PPTX
Million Monkeys User Group
PPTX
Strata 2012 Million Monkeys
PPTX
EC2 Performance, Spot Instance ROI and EMR Scalability
ODP
Why Use MVC?
ODP
How to Use MVC
PPT
Introduction to Android
Managing Real-Time Data Teams
Pulsar for Kafka People
Big Data and Analytics in the COVID-19 Era
Working Together As Data Teams V1
What Does an Exec Need to About Architecture and Why
The Five Dysfunctions of a Data Engineering Team
HBaseCon 2014-Just the Basics
Million Monkeys User Group
Strata 2012 Million Monkeys
EC2 Performance, Spot Instance ROI and EMR Scalability
Why Use MVC?
How to Use MVC
Introduction to Android

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Electronic commerce courselecture one. Pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Modernizing your data center with Dell and AMD
Unlocking AI with Model Context Protocol (MCP)
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Network Security Unit 5.pdf for BCA BBA.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Review of recent advances in non-invasive hemoglobin estimation
Understanding_Digital_Forensics_Presentation.pptx
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
Empathic Computing: Creating Shared Understanding
Machine learning based COVID-19 study performance prediction

Introduction to Regular Expressions

  • 2. What Are They? • Language to parse text • Apply logic and constraints • Concise (but not readable) • Consistent (mostly) • Widely supported in programming languages
  • 3. Hello Regex Source Text Regular Expression Yield “hello world” “hello” { “hello” } “hello world hello world” “hello” { “hello”, “hello” } “hello world hello world” “world” { “world”, “world” } “hello world hello world” “hello world” { “hello world”, “hello world” }
  • 4. Java Regex Code Pattern pattern = Pattern.compile("hello"); Matcher matcher = pattern.matcher("hello world"); // Find all matches while (matcher.find()) { // Get the matching string String match = matcher.group(); // match = “hello” }
  • 5. C# Regex Code foreach (Match match in Regex.Matches("hello world", "hello", RegexOptions.IgnoreCase)) { // Get the matching string String match = match.Value; // match = “hello” }
  • 6. Python Regex Code regex = re.compile("hello"); results = regex.search("hello world"); // results = "hello"
  • 7. Perl Regex Code $value = "hello world"; $value =~ m/hello/; $result = $1; // result = "hello"
  • 8. The (Ugly) Alternative String needle = "hello"; String haystack = "hello world hello world"; int index = 0; while ((index = haystack.indexOf( needle, index )) != -1) { String match = haystack.substring( index, index + needle.length() ); index++; }
  • 9. Regex Metacharacters • * - Match zero or more times • ? - Match zero or 1 time • + - Match one or more times • ^ - Match the start of a string • $ - Match the end of a string
  • 10. Character Classes POSIXPOSIX ShorthandShorthand LonghandLonghand DescriptionDescription [:word:] w [A-Za-z0-9_] Alphanumeric Chars. W [^A-Za-z0-9_] Non-alphanumeric Chars. [:alpha:] [A-Za-z] Alphabetic Chars. [:blank:] [ t] Space and tab [:digit:] d [0-9] Numeric Characters D [^0-9] Non-numeric Chars. [:space:] s [ trnvf] Whitespace Characters
  • 11. Groups Source TextSource Text Regular ExpressionRegular Expression YieldYield “hello world” “([a-z]+)s+([a-z]+) { “hello world”, “hello”, “world” } “hello world12345” “([a-z]+)s+([a-z]+) { “hello world”, “hello”, “world” } “hello world12345” “([a-z]+)s+([a-z]+)(d+) { “hello world12345”, “hello”, “world”, “12345” }
  • 12. Example • Example that parses, cleans up, and normalizes input
  • 13. Recommended Reading • Mastering Regular Expressions by Jeffry Friedl • Regular Expressions Cheat Sheet http://www.addedbytes.com/cheat-sheets/regular-e • Regex Evaluator http://www.cuneytyilmaz.com/prog/jrx/