SlideShare a Scribd company logo
1 of 4
Download to read offline
Regular Expressions


A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you
check if a particular string matches a given regular expression (or if a given regular expression matches a
particular string, which comes down to the same thing).



Regular expressions can be concatenated to form new regular expressions; if A and B are both regular
expressions, then AB is also a regular expression. In general, if a string p matches A and another string q
matches B, the string pq will match AB. This holds unless A or B contain low precedence operations;
boundary conditions between A and B; or have numbered group references.


Regular expressions can contain both special and ordinary characters. Most ordinary characters, like 'A', 'a',
or '0', are the simplest regular expressions; they simply match themselves. You can concatenate ordinary
characters, so last matches the string 'last'. (In the rest of this section, weu2019ll write REu2019s in this
special style, usually without quotes, and strings to be matched 'in single quotes'.)



Some characters, like '|' or '(', are special. Special characters either stand for classes of ordinary characters,
or affect how the regular expressions around them are interpreted. Regular expression pattern strings may
not contain null bytes, but can specify the null byte using a number notation such as 'x00'.
'.'

(Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag
has been specified, this matches any character including a newline.

'^'

(Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after
each newline.

'$'

Matches the end of the string or just before the newline at the end of the string, and in
MULTILINE mode also matches before a newline. foo matches both u2018foou2019 and
u2018foobaru2019, while the regular expression foo$ matches only u2018foou2019. More
interestingly, searching for foo.$ in 'foo1nfoo2n' matches u2018foo2u2019 normally, but
u2018foo1u2019 in MULTILINE mode; searching for a single $ in 'foon' will find two (empty)
matches: one just before the newline, and one at the end of the string.

'*'

Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions
as are possible. ab* will match u2018au2019, u2018abu2019, or u2018au2019 followed by
any number of u2018bu2019s.

'+'

Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match
u2018au2019 followed by any non-zero number of u2018bu2019s; it will not match just
u2018au2019.

'?'

Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either
u2018au2019 or u2018abu2019.

*?, +?, ??

The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this
behaviour isnu2019t desired; if the RE <.*> is matched against '<H1>title</H1>', it will match the
entire string, and not just '<H1>'. Adding '?' after the qualifier makes it perform the match in nongreedy or minimal fashion; as few characters as possible will be matched. Using .*? in the
previous expression will match only '<H1>'.

More Related Content

Viewers also liked

Representation of binary tree in memory
Representation of binary tree in memoryRepresentation of binary tree in memory
Representation of binary tree in memoryRohini Shinde
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithmKamal Nayan
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressionsBen Brumfield
 
моя през училища 2015 — копия
моя през училища 2015 — копиямоя през училища 2015 — копия
моя през училища 2015 — копияТетяна Карасюк
 
Elementi specialnoi teorii vidnosnosti
Elementi specialnoi teorii vidnosnosti Elementi specialnoi teorii vidnosnosti
Elementi specialnoi teorii vidnosnosti Ilona Bacurovska
 
АКТУАЛЬНЫЕ ЗАДАЧИ СТРУКТУР ВЛАСТИ И ОБЩЕСТВЕННОСТИ В РЕАЛИЗАЦИИ ЗАКОНА...
АКТУАЛЬНЫЕ  ЗАДАЧИ  СТРУКТУР ВЛАСТИ  И  ОБЩЕСТВЕННОСТИ  В  РЕАЛИЗАЦИИ  ЗАКОНА...АКТУАЛЬНЫЕ  ЗАДАЧИ  СТРУКТУР ВЛАСТИ  И  ОБЩЕСТВЕННОСТИ  В  РЕАЛИЗАЦИИ  ЗАКОНА...
АКТУАЛЬНЫЕ ЗАДАЧИ СТРУКТУР ВЛАСТИ И ОБЩЕСТВЕННОСТИ В РЕАЛИЗАЦИИ ЗАКОНА...KomitetGI
 
Hviliovi protsesi. hviliova optica 2 god
Hviliovi protsesi. hviliova optica 2  godHviliovi protsesi. hviliova optica 2  god
Hviliovi protsesi. hviliova optica 2 godIlona Bacurovska
 
Geometrichna optica. photometriya 2god.
Geometrichna optica. photometriya  2god. Geometrichna optica. photometriya  2god.
Geometrichna optica. photometriya 2god. Ilona Bacurovska
 
6.0 rdp sur raspberry
6.0 rdp sur raspberry6.0 rdp sur raspberry
6.0 rdp sur raspberryOsi Pallière
 
Sound designer unit 70 jess
Sound designer unit 70 jessSound designer unit 70 jess
Sound designer unit 70 jessjrcollins123
 
фотогалерея
фотогалереяфотогалерея
фотогалереяSerega2jcqydy2
 
International Journal of Humanities and Social Science Invention (IJHSSI)
International Journal of Humanities and Social Science Invention (IJHSSI)International Journal of Humanities and Social Science Invention (IJHSSI)
International Journal of Humanities and Social Science Invention (IJHSSI)inventionjournals
 
Proyecto Hotel Gardenia (IV): Restaurante
Proyecto Hotel Gardenia (IV): RestauranteProyecto Hotel Gardenia (IV): Restaurante
Proyecto Hotel Gardenia (IV): RestauranteInduo Collection
 
International Journal of Humanities and Social Science Invention (IJHSSI)
International Journal of Humanities and Social Science Invention (IJHSSI)International Journal of Humanities and Social Science Invention (IJHSSI)
International Journal of Humanities and Social Science Invention (IJHSSI)inventionjournals
 

Viewers also liked (16)

Representation of binary tree in memory
Representation of binary tree in memoryRepresentation of binary tree in memory
Representation of binary tree in memory
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithm
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
моя през училища 2015 — копия
моя през училища 2015 — копиямоя през училища 2015 — копия
моя през училища 2015 — копия
 
Elementi specialnoi teorii vidnosnosti
Elementi specialnoi teorii vidnosnosti Elementi specialnoi teorii vidnosnosti
Elementi specialnoi teorii vidnosnosti
 
АКТУАЛЬНЫЕ ЗАДАЧИ СТРУКТУР ВЛАСТИ И ОБЩЕСТВЕННОСТИ В РЕАЛИЗАЦИИ ЗАКОНА...
АКТУАЛЬНЫЕ  ЗАДАЧИ  СТРУКТУР ВЛАСТИ  И  ОБЩЕСТВЕННОСТИ  В  РЕАЛИЗАЦИИ  ЗАКОНА...АКТУАЛЬНЫЕ  ЗАДАЧИ  СТРУКТУР ВЛАСТИ  И  ОБЩЕСТВЕННОСТИ  В  РЕАЛИЗАЦИИ  ЗАКОНА...
АКТУАЛЬНЫЕ ЗАДАЧИ СТРУКТУР ВЛАСТИ И ОБЩЕСТВЕННОСТИ В РЕАЛИЗАЦИИ ЗАКОНА...
 
2 overview p lu g kpd peserta kursus
2 overview  p lu g kpd peserta kursus2 overview  p lu g kpd peserta kursus
2 overview p lu g kpd peserta kursus
 
Hviliovi protsesi. hviliova optica 2 god
Hviliovi protsesi. hviliova optica 2  godHviliovi protsesi. hviliova optica 2  god
Hviliovi protsesi. hviliova optica 2 god
 
Geometrichna optica. photometriya 2god.
Geometrichna optica. photometriya  2god. Geometrichna optica. photometriya  2god.
Geometrichna optica. photometriya 2god.
 
6.0 rdp sur raspberry
6.0 rdp sur raspberry6.0 rdp sur raspberry
6.0 rdp sur raspberry
 
Sound designer unit 70 jess
Sound designer unit 70 jessSound designer unit 70 jess
Sound designer unit 70 jess
 
фотогалерея
фотогалереяфотогалерея
фотогалерея
 
International Journal of Humanities and Social Science Invention (IJHSSI)
International Journal of Humanities and Social Science Invention (IJHSSI)International Journal of Humanities and Social Science Invention (IJHSSI)
International Journal of Humanities and Social Science Invention (IJHSSI)
 
Proyecto Hotel Gardenia (IV): Restaurante
Proyecto Hotel Gardenia (IV): RestauranteProyecto Hotel Gardenia (IV): Restaurante
Proyecto Hotel Gardenia (IV): Restaurante
 
International Journal of Humanities and Social Science Invention (IJHSSI)
International Journal of Humanities and Social Science Invention (IJHSSI)International Journal of Humanities and Social Science Invention (IJHSSI)
International Journal of Humanities and Social Science Invention (IJHSSI)
 
Improve english
Improve englishImprove english
Improve english
 

Similar to Regular Expressions Explained

Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netProgrammer Blog
 
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
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfBryan Alejos
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular ExpressionBinsent Ribera
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RHellen Gakuruh
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)Chirag Shetty
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 TrainingChris Chubb
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionskeeyre
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionKuyseng Chhoeun
 

Similar to Regular Expressions Explained (20)

Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.net
 
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
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheet
 
To lec 03
To lec 03To lec 03
To lec 03
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions
 
python_strings.pdf
python_strings.pdfpython_strings.pdf
python_strings.pdf
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
 

More from Raghu nath

Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)Raghu nath
 
Javascript part1
Javascript part1Javascript part1
Javascript part1Raghu nath
 
Selection sort
Selection sortSelection sort
Selection sortRaghu nath
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)Raghu nath
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithmsRaghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp roleRaghu nath
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4Raghu nath
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3Raghu nath
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 
Adv excel® 2013
Adv excel® 2013Adv excel® 2013
Adv excel® 2013Raghu nath
 

More from Raghu nath (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)
 
MS WORD 2013
MS WORD 2013MS WORD 2013
MS WORD 2013
 
Msword
MswordMsword
Msword
 
Ms word
Ms wordMs word
Ms word
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Selection sort
Selection sortSelection sort
Selection sort
 
Binary search
Binary search Binary search
Binary search
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithms
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp role
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Perl
PerlPerl
Perl
 
Adv excel® 2013
Adv excel® 2013Adv excel® 2013
Adv excel® 2013
 

Regular Expressions Explained

  • 2.  A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).  Regular expressions can be concatenated to form new regular expressions; if A and B are both regular expressions, then AB is also a regular expression. In general, if a string p matches A and another string q matches B, the string pq will match AB. This holds unless A or B contain low precedence operations; boundary conditions between A and B; or have numbered group references.
  • 3.  Regular expressions can contain both special and ordinary characters. Most ordinary characters, like 'A', 'a', or '0', are the simplest regular expressions; they simply match themselves. You can concatenate ordinary characters, so last matches the string 'last'. (In the rest of this section, weu2019ll write REu2019s in this special style, usually without quotes, and strings to be matched 'in single quotes'.)  Some characters, like '|' or '(', are special. Special characters either stand for classes of ordinary characters, or affect how the regular expressions around them are interpreted. Regular expression pattern strings may not contain null bytes, but can specify the null byte using a number notation such as 'x00'.
  • 4. '.' (Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline. '^' (Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline. '$' Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline. foo matches both u2018foou2019 and u2018foobaru2019, while the regular expression foo$ matches only u2018foou2019. More interestingly, searching for foo.$ in 'foo1nfoo2n' matches u2018foo2u2019 normally, but u2018foo1u2019 in MULTILINE mode; searching for a single $ in 'foon' will find two (empty) matches: one just before the newline, and one at the end of the string. '*' Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions as are possible. ab* will match u2018au2019, u2018abu2019, or u2018au2019 followed by any number of u2018bu2019s. '+' Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match u2018au2019 followed by any non-zero number of u2018bu2019s; it will not match just u2018au2019. '?' Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either u2018au2019 or u2018abu2019. *?, +?, ?? The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isnu2019t desired; if the RE <.*> is matched against '<H1>title</H1>', it will match the entire string, and not just '<H1>'. Adding '?' after the qualifier makes it perform the match in nongreedy or minimal fashion; as few characters as possible will be matched. Using .*? in the previous expression will match only '<H1>'.