SlideShare a Scribd company logo
1 of 62
Regular @ !#?@ !
Expressions 101
KNOW YOUR ENEMY
rubular.com
Part I
Character Literals and Wildcards
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/m/
http://tinyurl.com/rx101-2
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/me/
http://tinyurl.com/rx101-4
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/./
http://tinyurl.com/rx101-5
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/./
http://tinyurl.com/rx101-8
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/.e/
http://tinyurl.com/rx101-10
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/[aeiou]/
http://tinyurl.com/rx101-11
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/[a-f]/
http://tinyurl.com/rx101-12
/[A-z]/?
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/[a-fw-z]/
http://tinyurl.com/rx101-13
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/[^A-Z]/
http://tinyurl.com/rx101-14
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/d/
http://tinyurl.com/rx101-15
"Now_is_the_time_for_all_good_men_to_come_to_the_aid_of_their_country.
"_was_proposed_as_a_typing_drill_by_a_teacher_named_Charles_E._Weller
_aka_"Chase"_in_1918.

/s/
http://tinyurl.com/rx101-16
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/w/
http://tinyurl.com/rx101-17
"Now_is_the_time_for_all_good_men_to_come_to_the_aid_of_their_country.
"_was_proposed_as_a_typing_drill_by_a_teacher_named_Charles_E._Weller
_aka_"Chase"_in_1918.

/W/
http://tinyurl.com/rx101-18
Activity!
Write a regular expression to match
the telephone number in this text:

For a good time call Stanley: 555-1212 - you will not be disappoint.

http://tinyurl.com/rx101-a1
Part II
Quantifiers
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/men?/
http://tinyurl.com/rx101-19
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/".*"/
http://tinyurl.com/rx101-20
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/".*?"/
http://tinyurl.com/rx101-21
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

“TMTOWTDI”
http://tinyurl.com/rx101-22

/"[^"]*"/
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/tw+[aeiou]/
http://tinyurl.com/rx101-23
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/tw*[aeiou]/
http://tinyurl.com/rx101-24
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/[aeiou]{2}/
http://tinyurl.com/rx101-25
squee squeee squeeee squeeeee squeeeeee!

/sque{3,4}/
http://tinyurl.com/rx101-26
squee squeee squeeee squeeeee squeeeeee!

/sque{4,}/
http://tinyurl.com/rx101-27
squee squeee squeeee squeeeee squeeeeee!

/sque{,2}/
http://tinyurl.com/rx101-28
“TMTOWTDI”
/men?/

=

/men{0,1}/
“TMTOWTDI”
/.*/

=

/.{0,}/
“TMTOWTDI”
/.+/

=

/.{1,}/
Activity!
Rewrite the telephone number regular expression
to use quantifiers.
For a good time call Stanley: 555-1212 - you will not be disappoint.
http://tinyurl.com/rx101-a1
Extra credit - extract the email address.
For a good time email stanley@aol.com you will not be disappoint.
http://tinyurl.com/rx101-a2
Part III
Anchors
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/^[^s]+/
http://tinyurl.com/rx101-29
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/[^s]+$/
http://tinyurl.com/rx101-30
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/baw*/
http://tinyurl.com/rx101-31
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/bw{3}b/
http://tinyurl.com/rx101-32
Activity!
Go back and fix this example to find whole
words that start with ‘t’ and end in a vowel.
http://tinyurl.com/rx101-24
Part IV
Match Groups
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

1. Now is the time for all good men to come to the aid of their country.
2. Chase

/"(.*?)"/
http://tinyurl.com/rx101-33
Replacement

str = '"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.'puts str.gsub(/"(.*?)"/, '<i>1</i>')
str = '"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.'puts str.gsub(/"(.*?)"/, '<i>1</i>')

<i>Now is the time for all good men to come to the aid of their country.</i> was
proposed as a typing drill by a teacher named Charles E. Weller aka
<i>Chase</i> in 1918.
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.
1. is
2. the
3. the
4. of

/b(the|is|of)b/

http://tinyurl.com/rx101-34
"Now is the time for all good men to come to the aid of their country." was
proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in
1918.

/b(?:the|is|of)b/

http://tinyurl.com/rx101-35
Walkthrough!

http://tinyurl.com/rx101-a3
Part V
Flags
Perl / PCRE Style

delimiters

/cat/gi
flags
regex
Java Style

Java

delimiters

glue

Pattern.compile(“cat”, CASE_INSENSITIVE | DOTALL);
regex

flags
i

CASE_INSENSITIVE

s

DOTALL

m

MULTILINE

g
Part VI
How do I use my powers?
grep
aka cheat at Letterpress!
Your Text Editor!
$str = 'three cats chased five mice';$str =~ /(cats|mice)/;print $1;$str =~ s/
(cats|mice)/fuzzy $1/g;print $str;

cats
three fuzzy cats chased five fuzzy mice
Script
var str = 'three cats chased five mice';var matches = str.match(/(cats|
mice)/);console.log( matches[1] );str.replace(/(cats|mice)/g, ‘fuzzy
$1’);console.log( str );

cats
three fuzzy cats chased five fuzzy mice
str = 'three cats chased five mice'matches = str.match(/(cats|mice)/)puts
matches[1]str.gsub!(/(cats|mice)/, ‘fuzzy 1’)puts str

cats
three fuzzy cats chased five fuzzy mice
String str = “three cats chased five mice”;
Pattern p = Pattern.compile(“(cats|mice)”);
Matcher m = p.matcher( str );System.out.println( m.group(1) );String newStr =
str.replaceAll(“(cats|mice)”, “fuzzy $1”);System.out.println( str );

cats
three fuzzy cats chased five fuzzy mice
String str = “three cats chased five mice”;
Pattern p = Pattern.compile(“bw{4}b”);
Matcher m = p.matcher( str );System.out.println( m.group(1) );
String str = “three cats chased five mice”;
Pattern p = Pattern.compile(“bw{4}b”);
Matcher m = p.matcher( str );System.out.println( m.group(1) );

No.
String str = “three cats chased five mice”;
Pattern p = Pattern.compile(“bw{4}b”);
Matcher m = p.matcher( str );System.out.println( m.group(1) );
http://www.regexplanet.com/advanced/java/index.html

More Related Content

More from Pawel Szymczykowski

More from Pawel Szymczykowski (6)

Las Vegas Mini Maker Faire Community Meeting 2.0
Las Vegas Mini Maker Faire Community Meeting 2.0Las Vegas Mini Maker Faire Community Meeting 2.0
Las Vegas Mini Maker Faire Community Meeting 2.0
 
Las Vegas Mini Maker Faire Community Meeting
Las Vegas Mini Maker Faire Community MeetingLas Vegas Mini Maker Faire Community Meeting
Las Vegas Mini Maker Faire Community Meeting
 
How was usrlib formed?
How was usrlib formed?How was usrlib formed?
How was usrlib formed?
 
Hackers inspace
Hackers inspaceHackers inspace
Hackers inspace
 
Instant ramen pawel szymczykowski
Instant ramen   pawel szymczykowskiInstant ramen   pawel szymczykowski
Instant ramen pawel szymczykowski
 
/usr/lib allhands
/usr/lib allhands/usr/lib allhands
/usr/lib allhands
 

Recently uploaded

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Regular expressions

  • 1. Regular @ !#?@ ! Expressions 101
  • 3.
  • 6. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /m/ http://tinyurl.com/rx101-2
  • 7. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /me/ http://tinyurl.com/rx101-4
  • 8. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /./ http://tinyurl.com/rx101-5
  • 9. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /./ http://tinyurl.com/rx101-8
  • 10. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /.e/ http://tinyurl.com/rx101-10
  • 11. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /[aeiou]/ http://tinyurl.com/rx101-11
  • 12. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /[a-f]/ http://tinyurl.com/rx101-12
  • 14. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /[a-fw-z]/ http://tinyurl.com/rx101-13
  • 15. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /[^A-Z]/ http://tinyurl.com/rx101-14
  • 16. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /d/ http://tinyurl.com/rx101-15
  • 18. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /w/ http://tinyurl.com/rx101-17
  • 20. Activity! Write a regular expression to match the telephone number in this text: For a good time call Stanley: 555-1212 - you will not be disappoint. http://tinyurl.com/rx101-a1
  • 22. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /men?/ http://tinyurl.com/rx101-19
  • 23. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /".*"/ http://tinyurl.com/rx101-20
  • 24. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /".*?"/ http://tinyurl.com/rx101-21
  • 25. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. “TMTOWTDI” http://tinyurl.com/rx101-22 /"[^"]*"/
  • 26. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /tw+[aeiou]/ http://tinyurl.com/rx101-23
  • 27. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /tw*[aeiou]/ http://tinyurl.com/rx101-24
  • 28. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /[aeiou]{2}/ http://tinyurl.com/rx101-25
  • 29. squee squeee squeeee squeeeee squeeeeee! /sque{3,4}/ http://tinyurl.com/rx101-26
  • 30. squee squeee squeeee squeeeee squeeeeee! /sque{4,}/ http://tinyurl.com/rx101-27
  • 31. squee squeee squeeee squeeeee squeeeeee! /sque{,2}/ http://tinyurl.com/rx101-28
  • 35. Activity! Rewrite the telephone number regular expression to use quantifiers. For a good time call Stanley: 555-1212 - you will not be disappoint. http://tinyurl.com/rx101-a1 Extra credit - extract the email address. For a good time email stanley@aol.com you will not be disappoint. http://tinyurl.com/rx101-a2
  • 37. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /^[^s]+/ http://tinyurl.com/rx101-29
  • 38. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /[^s]+$/ http://tinyurl.com/rx101-30
  • 39. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /baw*/ http://tinyurl.com/rx101-31
  • 40. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /bw{3}b/ http://tinyurl.com/rx101-32
  • 41. Activity! Go back and fix this example to find whole words that start with ‘t’ and end in a vowel. http://tinyurl.com/rx101-24
  • 43. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. 1. Now is the time for all good men to come to the aid of their country. 2. Chase /"(.*?)"/ http://tinyurl.com/rx101-33
  • 44. Replacement str = '"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.'puts str.gsub(/"(.*?)"/, '<i>1</i>') str = '"Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918.'puts str.gsub(/"(.*?)"/, '<i>1</i>') <i>Now is the time for all good men to come to the aid of their country.</i> was proposed as a typing drill by a teacher named Charles E. Weller aka <i>Chase</i> in 1918.
  • 45. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. 1. is 2. the 3. the 4. of /b(the|is|of)b/ http://tinyurl.com/rx101-34
  • 46. "Now is the time for all good men to come to the aid of their country." was proposed as a typing drill by a teacher named Charles E. Weller aka "Chase" in 1918. /b(?:the|is|of)b/ http://tinyurl.com/rx101-35
  • 49. Perl / PCRE Style delimiters /cat/gi flags regex
  • 52. Part VI How do I use my powers?
  • 53. grep aka cheat at Letterpress!
  • 55. $str = 'three cats chased five mice';$str =~ /(cats|mice)/;print $1;$str =~ s/ (cats|mice)/fuzzy $1/g;print $str; cats three fuzzy cats chased five fuzzy mice
  • 56. Script var str = 'three cats chased five mice';var matches = str.match(/(cats| mice)/);console.log( matches[1] );str.replace(/(cats|mice)/g, ‘fuzzy $1’);console.log( str ); cats three fuzzy cats chased five fuzzy mice
  • 57. str = 'three cats chased five mice'matches = str.match(/(cats|mice)/)puts matches[1]str.gsub!(/(cats|mice)/, ‘fuzzy 1’)puts str cats three fuzzy cats chased five fuzzy mice
  • 58. String str = “three cats chased five mice”; Pattern p = Pattern.compile(“(cats|mice)”); Matcher m = p.matcher( str );System.out.println( m.group(1) );String newStr = str.replaceAll(“(cats|mice)”, “fuzzy $1”);System.out.println( str ); cats three fuzzy cats chased five fuzzy mice
  • 59. String str = “three cats chased five mice”; Pattern p = Pattern.compile(“bw{4}b”); Matcher m = p.matcher( str );System.out.println( m.group(1) );
  • 60. String str = “three cats chased five mice”; Pattern p = Pattern.compile(“bw{4}b”); Matcher m = p.matcher( str );System.out.println( m.group(1) ); No.
  • 61. String str = “three cats chased five mice”; Pattern p = Pattern.compile(“bw{4}b”); Matcher m = p.matcher( str );System.out.println( m.group(1) );

Editor's Notes

  1. Feedback: - Move flags slide to before walkthrough - How about an ending slide?
  2. Kleene, Stephen C. (1956). &quot;Representation of Events in Nerve Nets and Finite Automata&quot;
  3. Character literals
  4. Multiple character literals
  5. The wildcard
  6. What if you actually want a period? Escape it! The same goes for any special character.
  7. You can combine wildcards with literals
  8. If you put something in brackets, it means ‘anything in the brackets’
  9. ..and you can do ranges by adding a hyphen
  10. Note that when using big ranges like A-z, that you are using ASCII ordering, so you’ll probably be including some things you don’t expect.
  11. ..or multiple ranges
  12. Brackets with a caret negates the match.. so here, select anything except for capital letters.
  13. Common character classes have shortcuts, like ‘d’ for digits.
  14. ‘s’ for spaces (including tabs, spaces and newlines)
  15. ‘w’ for WORD characters (including a-z, 0-9 and _)! You can remember them by thinking of your favorite shoe marketplace, DSW!
  16. You can invert any of those by using an upper case version.. so here we are selecting non-word characters
  17. Quantifiers are a special syntax that dictates how many of a thing to match
  18. Question mark means zero or one of the preceding character. Here, read this is ‘me’ followed by an optional ‘n’
  19. Asterisk aka glob, aka the ‘Kleene Star’ means zero or more of the preceding character. It is ridiculously powerful and hungry, like Sinistar. Here we are matching everything between quotes. But you can see a problem, that we’re also blowing past two other quotes on the way to the last one. This is called a greedy glob.
  20. We can limit the greed of the match by adding a question mark.. this will now stop matching as soon as the condition is met.
  21. BUT, we can also write it this way. TMTOWTDI
  22. + means one or more of the preceding character. Here we are matching t followed by one or more word characters, followed by a vowel. Note that we do not match ‘to’
  23. And here you can see the difference between + and *
  24. To specify an exact number, you can put the number in curly braces! Here we are selecting all double vowels.
  25. You can specify a minimum and maximum.. here we are looking for 3 or 4 e’s to zone on the perfect level of excitement.
  26. If you leave off one of ends, it works as a greater than or less than. Here we are looking for squ followed by a minimum of 4 e’s
  27. If you leave off one of ends, it works as a greater than or less than. Here we are looking for squ followed by a minimum of 4 e’s
  28. Anchors constrain your match to a certain part of the string!
  29. Caret means, at the beginning of the string. Don’t confuse it with the caret inside of brackets which means to negate. Here we are selecting the first ‘word’ in the string.
  30. Dollar sign means at the end of the string and goes on the other end.
  31.  is the best one EVER. It means a word boundary (start or end of the string, surrounded by spaces or punctuation). Here we’re matching all words that start with ‘a’ - but don’t match ‘a’ in the middle of string. TODO: How to do this without word boundaries?
  32. You can use it on both ends of your regex. Here we’re finding all words that are exactly three characters long.
  33. Anchors constrain your match to a certain part of the string!
  34. If you put something in parenthesis, you’re putting it into a match group or and storing the parenthesized content for later in a numbered slot
  35. Why do you care about storing them? You can reference them again later in code. (TODO: Change to &lt;blockquote&gt; tag and use javascripts. Use rubular on a workspace.
  36. If you use a pipe character, you can provide several options in a match group
  37. If you want to group things without creating match groups, you can use a special syntax with a questionable colon. Provide example of nested matchgroup spaghetti and why you would want to use this.
  38. Chances are if you know perl, you shouldn’t be here..
  39. One gotcha with Java is that it doesn’t contain a native regular expression delimiter so you have to double escape things.