SlideShare a Scribd company logo
1 of 19
Download to read offline
Regular Expression
101 ~ 1001
cmj / 2019-05 @HST
Regular Expression
• RegEx

• PCRE (Perl Compatible Regular Expression)

• Perl and popular
Finite-State Machine (FSM)
• Input (Entry state)

• State / Transition

• Accept
Example
• Try match aab

• First two a and follow one b

• Accept

• aaaaaabbbbb

• aab

• Reject

• ab

• abb
a
a
bb
Grammar I
• . Any character except NEWLINE

• a The character a

•  Escapes a special character

• a b f n r t v 0

• YYY

• xXX
Grammar II
• a | b The pattern a or pattern b

• a{x, y} x to y number of a

• [abc] One character of a, b or c

• a* Zero or more a

• a+ One or more a

• a? Zero or one a

• reluctant a*? a+?

• greedy a.+
Grammar III
• 0-9 Digit from 0 to 9

• d Digit (0 -9)

• D Not digit

• s One whitespace

• S Not whitespace
Grammar IV
• (…) Group of pattern

• (?: …) Not-capture group

• (?>…) Atomic group

• (?|…) Duplicate group

• (?P<name>…) Named a group

• (?P=name) Backreference to named group
Grammar V
• (?=…) Positive lookahead

• (?!…) Negative lookahead

• (?<=…) Positive lookbehind

• (?<!…) Negative lookbehind
Exercise
Exercise 1
• HTML tag

• find all <…> format and first appear word

• <(w+).*?>
Exercise 2
• Birthday validator

• Date format

• MM-DD-YYYY d{2}-d{2}-d{4}
https://en.wikipedia.org/wiki/Date_format_by_country
Exercise 2
• Birthday validator

• Date format

• MM-DD-YYYY d{2}-d{2}-d{4}

• ^(?:(?:(?:0?[13578]|1[02])(/|-|.)31)1|(?:(?:0?[1,3-9]|1[0-2])
(/|-|.)(?:29|30)2))(?:(?:1[6-9]|[2-9]d)?d{2})$|^(?:0?2(/|-|
.)293(?:(?:(?:1[6-9]|[2-9]d)?(?:0[48]|[2468][048]|[13579]
[26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:
1[0-2]))(/|-|.)(?:0?[1-9]|1d|2[0-8])4(?:(?:1[6-9]|[2-9]d)?
d{2})$
https://stackoverflow.com/questions/51224/regular-expression-to-match-valid-dates/8768241#8768241
Exercise 3
• ETH address

• 0x prefix with extra 40 characters

• 0x[a-zA-Z0-9]{40}

• BTC address

• ^[13][a-km-zA-Hj_Np-Z1-9]{25,34}
https://stackoverflow.com/questions/21683680/regex-to-match-bitcoin-addresses
Exercise 4
• Any imgur link

• https?://i.imgur.com/.*.(png|jpg|gif)

• https?://i.imgur.com/.*?.(png|jpg|gif)
Exercise 5
• Difference between

• ab(ab)

• (?<=ab)ab
Exercise 5
• Difference between

• ab(ab)

• (?<=ab)ab
Exercise 5
• Difference between

• ab(ab)

• (?<=ab)ab
No Q&A

More Related Content

More from Chia-Hao Tsai

Rootkit 102 - Kernel-Based Rootkit
Rootkit 102 - Kernel-Based RootkitRootkit 102 - Kernel-Based Rootkit
Rootkit 102 - Kernel-Based RootkitChia-Hao Tsai
 
Rootkit 101 - 2nd Edition
Rootkit 101 - 2nd EditionRootkit 101 - 2nd Edition
Rootkit 101 - 2nd EditionChia-Hao Tsai
 
Learn Python in 30 min - 4
Learn Python in 30 min - 4Learn Python in 30 min - 4
Learn Python in 30 min - 4Chia-Hao Tsai
 
Learn python in 30 min - 3
Learn python in 30 min - 3Learn python in 30 min - 3
Learn python in 30 min - 3Chia-Hao Tsai
 
Learn python 2 - Real World Case
Learn python 2 - Real World CaseLearn python 2 - Real World Case
Learn python 2 - Real World CaseChia-Hao Tsai
 
Passwd crack introduction
Passwd crack   introductionPasswd crack   introduction
Passwd crack introductionChia-Hao Tsai
 
Security coding c and c++ ch8(2)
Security coding c and c++   ch8(2)Security coding c and c++   ch8(2)
Security coding c and c++ ch8(2)Chia-Hao Tsai
 
Security coding c and c++ ch8 (1)
Security coding c and c++   ch8 (1)Security coding c and c++   ch8 (1)
Security coding c and c++ ch8 (1)Chia-Hao Tsai
 

More from Chia-Hao Tsai (14)

Rootkit 102 - Kernel-Based Rootkit
Rootkit 102 - Kernel-Based RootkitRootkit 102 - Kernel-Based Rootkit
Rootkit 102 - Kernel-Based Rootkit
 
Rootkit 101 - 2nd Edition
Rootkit 101 - 2nd EditionRootkit 101 - 2nd Edition
Rootkit 101 - 2nd Edition
 
ELF 101
ELF 101ELF 101
ELF 101
 
Maker - WiFi AP
Maker - WiFi APMaker - WiFi AP
Maker - WiFi AP
 
Learn Python in 30 min - 4
Learn Python in 30 min - 4Learn Python in 30 min - 4
Learn Python in 30 min - 4
 
Learn python in 30 min - 3
Learn python in 30 min - 3Learn python in 30 min - 3
Learn python in 30 min - 3
 
Learn python 2 - Real World Case
Learn python 2 - Real World CaseLearn python 2 - Real World Case
Learn python 2 - Real World Case
 
Learn python 1
Learn python 1Learn python 1
Learn python 1
 
HoneyCon 2014
HoneyCon 2014HoneyCon 2014
HoneyCon 2014
 
Passwd crack introduction
Passwd crack   introductionPasswd crack   introduction
Passwd crack introduction
 
Security coding c and c++ ch8(2)
Security coding c and c++   ch8(2)Security coding c and c++   ch8(2)
Security coding c and c++ ch8(2)
 
Security coding c and c++ ch8 (1)
Security coding c and c++   ch8 (1)Security coding c and c++   ch8 (1)
Security coding c and c++ ch8 (1)
 
Build web server
Build web serverBuild web server
Build web server
 
Rootkit tw(0224)
Rootkit tw(0224)Rootkit tw(0224)
Rootkit tw(0224)
 

Recently uploaded

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 

Recently uploaded (20)

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 

[2019.05] HST - RegEx 101 ~ 1001

  • 1. Regular Expression 101 ~ 1001 cmj / 2019-05 @HST
  • 2. Regular Expression • RegEx • PCRE (Perl Compatible Regular Expression) • Perl and popular
  • 3. Finite-State Machine (FSM) • Input (Entry state) • State / Transition • Accept
  • 4. Example • Try match aab • First two a and follow one b • Accept • aaaaaabbbbb • aab • Reject • ab • abb a a bb
  • 5. Grammar I • . Any character except NEWLINE • a The character a • Escapes a special character • a b f n r t v 0 • YYY • xXX
  • 6. Grammar II • a | b The pattern a or pattern b • a{x, y} x to y number of a • [abc] One character of a, b or c • a* Zero or more a • a+ One or more a • a? Zero or one a • reluctant a*? a+? • greedy a.+
  • 7. Grammar III • 0-9 Digit from 0 to 9 • d Digit (0 -9) • D Not digit • s One whitespace • S Not whitespace
  • 8. Grammar IV • (…) Group of pattern • (?: …) Not-capture group • (?>…) Atomic group • (?|…) Duplicate group • (?P<name>…) Named a group • (?P=name) Backreference to named group
  • 9. Grammar V • (?=…) Positive lookahead • (?!…) Negative lookahead • (?<=…) Positive lookbehind • (?<!…) Negative lookbehind
  • 11. Exercise 1 • HTML tag • find all <…> format and first appear word • <(w+).*?>
  • 12. Exercise 2 • Birthday validator • Date format • MM-DD-YYYY d{2}-d{2}-d{4} https://en.wikipedia.org/wiki/Date_format_by_country
  • 13. Exercise 2 • Birthday validator • Date format • MM-DD-YYYY d{2}-d{2}-d{4} • ^(?:(?:(?:0?[13578]|1[02])(/|-|.)31)1|(?:(?:0?[1,3-9]|1[0-2]) (/|-|.)(?:29|30)2))(?:(?:1[6-9]|[2-9]d)?d{2})$|^(?:0?2(/|-| .)293(?:(?:(?:1[6-9]|[2-9]d)?(?:0[48]|[2468][048]|[13579] [26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?: 1[0-2]))(/|-|.)(?:0?[1-9]|1d|2[0-8])4(?:(?:1[6-9]|[2-9]d)? d{2})$ https://stackoverflow.com/questions/51224/regular-expression-to-match-valid-dates/8768241#8768241
  • 14. Exercise 3 • ETH address • 0x prefix with extra 40 characters • 0x[a-zA-Z0-9]{40} • BTC address • ^[13][a-km-zA-Hj_Np-Z1-9]{25,34} https://stackoverflow.com/questions/21683680/regex-to-match-bitcoin-addresses
  • 15. Exercise 4 • Any imgur link • https?://i.imgur.com/.*.(png|jpg|gif) • https?://i.imgur.com/.*?.(png|jpg|gif)
  • 16. Exercise 5 • Difference between • ab(ab) • (?<=ab)ab
  • 17. Exercise 5 • Difference between • ab(ab) • (?<=ab)ab
  • 18. Exercise 5 • Difference between • ab(ab) • (?<=ab)ab