SlideShare uma empresa Scribd logo
1 de 12
REGULAR EXPRESSION IN
ACTION
Brief overview of Regular Expression building blocks and
tools with a practical example
REGULAR EXPRESSIONS PROVIDE A CONCISE AND FLEXIBLE MEANS FOR MATCHING STRINGS OF
TEXT, SUCH AS PARTICULAR CHARACTERS, WORDS, OR PATTERNS OF CHARACTERS.
WHAT ARE REGULAR EXPRESSIONS
THE REGEX COACH IS A GRAPHICAL APPLICATION
FOR WINDOWS WHICH CAN BE USED TO
EXPERIMENT WITH REGULAR EXPRESSIONS
INTERACTIVELY.
◦ http://weitz.de/regex-coach/
Sublime Text is a text editor that has support of find and
replace using Regular Expressions.
Web based Regular Expressions tester.
◦ http://www.regular-expressions.info/
THE MOST BASIC REGULAR EXPRESSION CONSISTS OF A LITERAL
which behaves just like string matching. For e.g.
◦ cat will match cat in About cats and dogs.
Special characters known as meta characters needs to be escaped with a  in
regular expressions if they are used as part of a literal:
◦ dogs.will match dogs. in About cats and dogs.
Meta characters are:
◦ [  ^ $ . | ? * + ( ) {
WITH A "CHARACTER CLASS", ALSO CALLED "CHARACTER SET", YOU CAN TELL
THE REGEX ENGINE TO MATCH ONLY ONE OUT OF SEVERAL CHARACTERS.
FOR E.G.
◦ gr[ae]ywill match grey and gray both.
Ranges can be specified using dash. For e.g.
◦ [0-9]will match any digit from 0 to 9.
◦ [0-9a-fA-F]will match any single hexadecimal digit.
Caret after the opening square bracket will negate the character class.
• The result is that the character class will match any character that is not
• in the character class. For e.g.
◦ [^0-9] will match any thing except number.
◦ q[^u] will not match Iraq but it will match Iraq is a country
Meta characters works fine without escaping in Character classes. For e.g.
◦ [+*]is a valid expression and match either * or +.
There are some pre-defined character classes known as short hand
character classes:
◦ w stands for[A-Za-z0-9_]
◦ s stands for[ trn]
◦ d stands for[0-9]
If a character class is repeated by using the ?, * or + operators, the
entire character class will be repeated, and not just the character that it
matched. For e.g.
◦ [0-9]+ can match 837 as well as 222
◦ ([0-9])1+ will match 222 but not 837.
The famous dot “.” operator matches anything. For e.g.
◦ a.b will match abb, aab, a+b etc.
^ and $ are used to match start and end of regular expressions. For e.g.
◦ ^My.*.$ will match anything starting with My and ending with a dot.
Pipe operator is used to match a string against either its left or the right
part. For e.g.
◦ (cat|dog) can match both cat or dog.
Question:
◦ If the expression is Get|GetValue|Set|SetValue and string is SetValue.
What will this match and why?
◦ What if the expression becomes Get(Value)?|Set(Value)?
* or {0,} and+ or {1,} are used to control repititions.
Round brackets besides grouping part of a regular expression
together, also create a "backreference". A backreference stores the
matching part of the string matched by the part of the regular
expression inside the parentheses. For e.g.
◦ ([0-9])1+ will match 222 but not 837.
If backreference are not required, you can optimize this regular
expression Set(?:Value)?
Backreferences can be used in expressions itself or in replacement
text. For e.g.
◦ <([A-Za-z][A-Za-z0-9]*)>.*</1>will match matching opening and closing tags.
/i makes the regex match case insensitive.
◦ [A-Z] will match A and a with this modifier.
/s enables "single-line mode". In this mode, the dot matches
newlines as well.
◦ .* will match sherazrnattari with this modifier.
/m enables "multi-line mode". In this mode, the caret and dollar
match before and after newlines in the subject string.
◦ .* will match only sherazin sherazrnattari with this modifier.
/x enables "free-spacing mode". In this mode, whitespace between
regex tokens is ignored, and an unescaped # starts a comment.
◦ #sherazrnrn.* will match only sheraz in with this modifier.
A conditional is a special construct that will first evaluate a lookaround, and then execute one
sub-regex if the lookaround succeeds, and another sub-regex if the lookaround fails.
Example of Positive lookahead is:
◦ q(?=uv*)will match q in quvvvv and qu.
Example of Negative lookahead is:
◦ q(?!uv*)will match q not followed by u and uv.
Example of Positive lookbehind is:
◦ (?<=b)awill match a prefixed by b like ba.
Example of Negative lookbehind is:
◦ (?<!b)awill match a not prefixed by b like ca and da etc.
abc… Letters
123… Digits
d Any Digit
D Any Non-digit character
. Any Character
. Period
[abc] Only a, b, or c
[^abc]Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
w Any Alphanumeric character
W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
s Any Whitespace
S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(abc|def) Matches abc or def
Most of the content is taken from
http://www.regular-expressions.info/
THANK YOU!

Mais conteúdo relacionado

Semelhante a Regular Expression Crash Course

Regular Expression in Action
Regular Expression in ActionRegular Expression in Action
Regular Expression in ActionFolio3 Software
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionskeeyre
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat SheetSydneyJohnson57
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式ji guang
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regexwayn
 
Regex startup
Regex startupRegex startup
Regex startupPayPal
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and YouJames Armes
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 

Semelhante a Regular Expression Crash Course (20)

Regular Expression in Action
Regular Expression in ActionRegular Expression in Action
Regular Expression in Action
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat Sheet
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
2.regular expressions
2.regular expressions2.regular expressions
2.regular expressions
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Regex startup
Regex startupRegex startup
Regex startup
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegEx
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 

Último

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Último (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Regular Expression Crash Course

  • 1. REGULAR EXPRESSION IN ACTION Brief overview of Regular Expression building blocks and tools with a practical example
  • 2. REGULAR EXPRESSIONS PROVIDE A CONCISE AND FLEXIBLE MEANS FOR MATCHING STRINGS OF TEXT, SUCH AS PARTICULAR CHARACTERS, WORDS, OR PATTERNS OF CHARACTERS. WHAT ARE REGULAR EXPRESSIONS
  • 3. THE REGEX COACH IS A GRAPHICAL APPLICATION FOR WINDOWS WHICH CAN BE USED TO EXPERIMENT WITH REGULAR EXPRESSIONS INTERACTIVELY. ◦ http://weitz.de/regex-coach/ Sublime Text is a text editor that has support of find and replace using Regular Expressions. Web based Regular Expressions tester. ◦ http://www.regular-expressions.info/
  • 4. THE MOST BASIC REGULAR EXPRESSION CONSISTS OF A LITERAL which behaves just like string matching. For e.g. ◦ cat will match cat in About cats and dogs. Special characters known as meta characters needs to be escaped with a in regular expressions if they are used as part of a literal: ◦ dogs.will match dogs. in About cats and dogs. Meta characters are: ◦ [ ^ $ . | ? * + ( ) {
  • 5. WITH A "CHARACTER CLASS", ALSO CALLED "CHARACTER SET", YOU CAN TELL THE REGEX ENGINE TO MATCH ONLY ONE OUT OF SEVERAL CHARACTERS. FOR E.G. ◦ gr[ae]ywill match grey and gray both. Ranges can be specified using dash. For e.g. ◦ [0-9]will match any digit from 0 to 9. ◦ [0-9a-fA-F]will match any single hexadecimal digit. Caret after the opening square bracket will negate the character class. • The result is that the character class will match any character that is not • in the character class. For e.g. ◦ [^0-9] will match any thing except number. ◦ q[^u] will not match Iraq but it will match Iraq is a country
  • 6. Meta characters works fine without escaping in Character classes. For e.g. ◦ [+*]is a valid expression and match either * or +. There are some pre-defined character classes known as short hand character classes: ◦ w stands for[A-Za-z0-9_] ◦ s stands for[ trn] ◦ d stands for[0-9] If a character class is repeated by using the ?, * or + operators, the entire character class will be repeated, and not just the character that it matched. For e.g. ◦ [0-9]+ can match 837 as well as 222 ◦ ([0-9])1+ will match 222 but not 837.
  • 7. The famous dot “.” operator matches anything. For e.g. ◦ a.b will match abb, aab, a+b etc. ^ and $ are used to match start and end of regular expressions. For e.g. ◦ ^My.*.$ will match anything starting with My and ending with a dot. Pipe operator is used to match a string against either its left or the right part. For e.g. ◦ (cat|dog) can match both cat or dog. Question: ◦ If the expression is Get|GetValue|Set|SetValue and string is SetValue. What will this match and why? ◦ What if the expression becomes Get(Value)?|Set(Value)? * or {0,} and+ or {1,} are used to control repititions.
  • 8. Round brackets besides grouping part of a regular expression together, also create a "backreference". A backreference stores the matching part of the string matched by the part of the regular expression inside the parentheses. For e.g. ◦ ([0-9])1+ will match 222 but not 837. If backreference are not required, you can optimize this regular expression Set(?:Value)? Backreferences can be used in expressions itself or in replacement text. For e.g. ◦ <([A-Za-z][A-Za-z0-9]*)>.*</1>will match matching opening and closing tags.
  • 9. /i makes the regex match case insensitive. ◦ [A-Z] will match A and a with this modifier. /s enables "single-line mode". In this mode, the dot matches newlines as well. ◦ .* will match sherazrnattari with this modifier. /m enables "multi-line mode". In this mode, the caret and dollar match before and after newlines in the subject string. ◦ .* will match only sherazin sherazrnattari with this modifier. /x enables "free-spacing mode". In this mode, whitespace between regex tokens is ignored, and an unescaped # starts a comment. ◦ #sherazrnrn.* will match only sheraz in with this modifier.
  • 10. A conditional is a special construct that will first evaluate a lookaround, and then execute one sub-regex if the lookaround succeeds, and another sub-regex if the lookaround fails. Example of Positive lookahead is: ◦ q(?=uv*)will match q in quvvvv and qu. Example of Negative lookahead is: ◦ q(?!uv*)will match q not followed by u and uv. Example of Positive lookbehind is: ◦ (?<=b)awill match a prefixed by b like ba. Example of Negative lookbehind is: ◦ (?<!b)awill match a not prefixed by b like ca and da etc.
  • 11. abc… Letters 123… Digits d Any Digit D Any Non-digit character . Any Character . Period [abc] Only a, b, or c [^abc]Not a, b, nor c [a-z] Characters a to z [0-9] Numbers 0 to 9 w Any Alphanumeric character W Any Non-alphanumeric character {m} m Repetitions {m,n} m to n Repetitions * Zero or more repetitions + One or more repetitions ? Optional character s Any Whitespace S Any Non-whitespace character ^…$ Starts and ends (…) Capture Group (a(bc)) Capture Sub-group (.*) Capture all (abc|def) Matches abc or def
  • 12. Most of the content is taken from http://www.regular-expressions.info/ THANK YOU!