SlideShare ist ein Scribd-Unternehmen logo
1 von 62
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Mehr von Pawel Szymczykowski

Mehr von 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
 

Kürzlich hochgeladen

Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sectoritnewsafrica
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentMahmoud Rabie
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 

Kürzlich hochgeladen (20)

Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career Development
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 

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) );

Hinweis der Redaktion

  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.