SlideShare a Scribd company logo
1 of 63
Download to read offline
PYTHON PROGRAMMING
Text Processing
XI. String Manipulation and Regular Expressions

Engr. Ranel O. Padon
PYTHON PROGRAMMING TOPICS
I

• Introduction to Python Programming

II

• Python Basics

III

• Controlling the Program Flow

IV

• Program Components: Functions, Classes, Packages, and Modules

V

• Sequences (List and Tuples), and Dictionaries

VI

• Object-Based Programming: Classes and Objects

VII

• Customizing Classes and Operator Overloading

VIII

• Object-Oriented Programming: Inheritance and Polymorphism

IX

• Randomization Algorithms

X

• Exception Handling and Assertions

XI

• String Manipulation and Regular Expressions

XII

• File Handling and Processing

XIII

• GUI Programming Using Tkinter
Text
Processing

String Manipulation
Regular Expressions
TEXT PROCESSING
* used to develop text editors, word processors,
page-layout soft-ware, computerized typesetting systems,
and other text-processing software
* used to search for patterns in text
* used to validate user-inputs
* used to process the contents of text files
STRING MANIPULATION

Strings are made up of Characters.
Characters are made up of:
Digits (0, 1, 2, …, 9)
Letters (a, b, c, …, z)
Symbols (@, *, #, $, %, &, …)
String
Methods
String
Methods
String
Methods
String
Methods
String
Methods
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
STRING MANIPULATION | Samples
REGULAR EXPRESSIONS

to test if a certain string contains a day of a week,
it has to test if it contains “Monday,” “Tuesday”, and so on.

you will need to use the find() method seven times
but, it could be solved elegantly by Regular Expressions
REGULAR EXPRESSIONS

* use string methods for simple text processing
* string methods are more readable and simpler
than regular expressions
REGULAR EXPRESSION
text pattern that a program uses to find substrings that will
match the required pattern
expression that specify a set of strings
a pattern matching mechanism
also known as Regex

introduced in the 1950s as part of formal language theory
REGULAR EXPRESSIONS
very powerful! hundreds of code could be reduced to
a one-liner elegant regular expression.
used to construct compilers, interpreters, text editors, …
used to search & match text patterns
used to validate text data formats especially input data
REGULAR EXPRESSIONS
Popular programming languages have RegEx capabilities:
Perl, JavaScript, PHP, Python, Ruby, Tcl,
Java, C, C++, C#, .Net, Ruby, …
REGEX
Popular programming languages have RegEx capabilities:
Perl, JavaScript, PHP, Python, Ruby, Tcl,
Java, C, C++, C#, .Net, Ruby, …
REGEX | General Concepts
 Alternative
 Grouping

 Quantification
 Anchors
 Meta-characters
 Character Classes
REGEX | General Concepts
 Alternative:

|

 Grouping:

()

 Quantification:

? + * {m,n}

 Anchors:

^$

 Meta-characters:

. [ ] [-] [^ ]

 Character Classes: w d s W …
REGEX | Alternative

“ranel|ranilio” == “ranel” or “ranilio”
“gray|grey” == “gray” or “grey”
REGEX | Grouping

“ran(el|ilio)” == “ranel” or “ranilio”
“gr(a|e)y” == “gray” or “grey”
“ra(mil|n(ny|el))” == “ramil” or “ranny” or “ranel”
REGEX | Quantification | ?

? == zero or one of the preceding element
“rani?el” == “raniel” or “ranel”
“colou?r” == “colour” or “color”
REGEX | Quantification | *

* == zero or more of the preceding element
“goo*gle” == “gogle” or “google” or “gooooogle”
“(ha)*” == “” or “ha” or “haha” or “hahahahaha”
“12*3” == “13” or “1223” or “12223”
REGEX | Quantification | +

+ == one or more of the preceding element
“goo+gle” == “google” or “gooogle” or “gooooogle”
“(ha)+” == “ha” or “haha” or “hahahahaha”
“12+3” == “123” or “1223” or “12223”
REGEX | Quantification | {m,n}

{m, n} == m to n times of the preceding element
“go{2, 3}gle” == “google” or “gooogle”
“6{3, 6}” == “666” or “6666” or “66666” or “666666”
“5{3}” == “555”
“a{2,}” == “aa” or “aaa” or “aaaa” or “aaaaa” …
REGEX | Anchors | ^
^ == matches the starting position within the string
“^laman” == “lamang” or “lamang-loob” or “lamang-lupa”

“^2013” == “2013”, “2013-12345”, “2013/1320”
REGEX | Anchors | $
$ == matches the ending position within the string
“laman$” == “halaman” or “kaalaman”

“2013$” == “2013”, “777-2013”, “0933-445-2013”
REGEX | Meta-characters | .

. == matches any single character
“ala.” == “ala” or “alat” or “alas” or “ala2”
“1.3” == “123” or “143” or “1s3”
REGEX | Meta-characters | [ ]

[ ] == matches a single character that is
contained within the brackets.
“[abc]” == “a” or “b” or “c”
“[aoieu]” == any vowel
“[0123456789]” == any digit
REGEX | Meta-characters | [ - ]
[ - ] == matches a single character that is
contained within the brackets
and the specified range.
“[a-c]” == “a” or “b” or “c”
“[a-z]” == all alphabet letters (lowercase only)

“[a-zA-Z]” == all letters (lowercase & uppercase)
“[0-9]” == all digits
REGEX | Meta-characters | [^ ]
[^ ] == matches a single character that is not contained
within the brackets.
“[^aeiou]” == any non-vowel
“[^0-9]” == any non-digit
“[^abc]” == any character, but not “a”, “b”, or “c”
REGEX | Character Classes
Character classes specifies a group of characters
to match in a string
REGEX | Summary
 Alternative:

|

 Grouping:

()

 Quantification:

? + * {m,n}

 Anchors:

^$

 Meta-characters:

. [ ] [-] [^ ]

 Character Classes: w d s W …
REGEX | Combo
REGEX | Date Validation

“1/3/2013” or “24/2/2020”
(d{1,2}/d{1,2}/d{4})
REGEX | Alphanumeric, -, & _

“rr2000” or “ranel_padon” or “Oblan-Padon”
([a-zA-Z0-9-_]+)
REGEX | Numbers in 1 to 50

“1” or “50” or “14”
(^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$)
REGEX | HTML Tags

“<title>” or “<strong>” or “/body”
(<(/?[^>]+)>)
PYTHON REGEX | Raw String
PYTHON REGEX | Raw String r
Two Solutions:
PYTHON REGEX | Raw String r
Raw Strings are used for enhancing readability.
PYTHON REGEX | Raw String
PYTHON REGEX | The re Module
PYTHON REGEX | Samples
PYTHON REGEX | Samples
PYTHON REGEX | Samples
PYTHON REGEX | Samples
PYTHON REGEX | Samples
PYTHON REGEX | Samples
PYTHON REGEX | Samples
PYTHON REGEX | Samples
PYTHON REGEX | Samples
REFERENCES
 Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

 Disclaimer: Most of the images/information used here have no proper source
citation, and I do not claim ownership of these either. I don’t want to reinvent the
wheel, and I just want to reuse and reintegrate materials that I think are useful or
cool, then present them in another light, form, or perspective. Moreover, the
images/information here are mainly used for illustration/educational purposes only,
in the spirit of openness of data, spreading light, and empowering people with
knowledge. 

More Related Content

What's hot (20)

Python list
Python listPython list
Python list
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
11 Unit 1 Chapter 02 Python Fundamentals
11  Unit 1 Chapter 02 Python Fundamentals11  Unit 1 Chapter 02 Python Fundamentals
11 Unit 1 Chapter 02 Python Fundamentals
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
String in java
String in javaString in java
String in java
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Python Data Types.pdf
Python Data Types.pdfPython Data Types.pdf
Python Data Types.pdf
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Data types in python
Data types in pythonData types in python
Data types in python
 

Viewers also liked

Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismRanel Padon
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingRanel Padon
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesRanel Padon
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On RandomnessRanel Padon
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowRanel Padon
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsRanel Padon
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File ProcessingRanel Padon
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with DrupalRanel Padon
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The BasicsRanel Padon
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingRanel Padon
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsRanel Padon
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. IntroductionRanel Padon
 

Viewers also liked (12)

Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator Overloading
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with Drupal
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
 

Similar to Python Programming - XI. String Manipulation and Regular Expressions

DerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
DerbyCon 7.0 Legacy: Regular Expressions (Regex) OverviewDerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
DerbyCon 7.0 Legacy: Regular Expressions (Regex) OverviewCiNPA Security SIG
 
OISF: Regular Expressions (Regex) Overview
OISF: Regular Expressions (Regex) OverviewOISF: Regular Expressions (Regex) Overview
OISF: Regular Expressions (Regex) OverviewCiNPA Security SIG
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29Bilal Ahmed
 
Scala Parser Combinators - Scalapeno Lightning Talk
Scala Parser Combinators - Scalapeno Lightning TalkScala Parser Combinators - Scalapeno Lightning Talk
Scala Parser Combinators - Scalapeno Lightning TalkLior Schejter
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Data Con LA
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupReuven Lerner
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...Adam Mukharil Bachtiar
 
Let's build a parser!
Let's build a parser!Let's build a parser!
Let's build a parser!Boy Baukema
 
Building DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchBuilding DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchEelco Visser
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , OverviewNB Veeresh
 
AestasIT - Internal DSLs in Scala
AestasIT - Internal DSLs in ScalaAestasIT - Internal DSLs in Scala
AestasIT - Internal DSLs in ScalaDmitry Buzdin
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHPShweta A
 

Similar to Python Programming - XI. String Manipulation and Regular Expressions (20)

DerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
DerbyCon 7.0 Legacy: Regular Expressions (Regex) OverviewDerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
DerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
 
OISF: Regular Expressions (Regex) Overview
OISF: Regular Expressions (Regex) OverviewOISF: Regular Expressions (Regex) Overview
OISF: Regular Expressions (Regex) Overview
 
R language
R languageR language
R language
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
 
Scala Parser Combinators - Scalapeno Lightning Talk
Scala Parser Combinators - Scalapeno Lightning TalkScala Parser Combinators - Scalapeno Lightning Talk
Scala Parser Combinators - Scalapeno Lightning Talk
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship group
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
 
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
 
Let's build a parser!
Let's build a parser!Let's build a parser!
Let's build a parser!
 
Json the-x-in-ajax1588
Json the-x-in-ajax1588Json the-x-in-ajax1588
Json the-x-in-ajax1588
 
Regular expression for everyone
Regular expression for everyoneRegular expression for everyone
Regular expression for everyone
 
Building DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchBuilding DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language Workbench
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , Overview
 
AestasIT - Internal DSLs in Scala
AestasIT - Internal DSLs in ScalaAestasIT - Internal DSLs in Scala
AestasIT - Internal DSLs in Scala
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 

More from Ranel Padon

The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)Ranel Padon
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with DrupalRanel Padon
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleRanel Padon
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Ranel Padon
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationRanel Padon
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryRanel Padon
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Ranel Padon
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Ranel Padon
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with DrupalRanel Padon
 

More from Ranel Padon (9)

The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with Drupal
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views Module
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputation
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQuery
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with Drupal
 

Recently uploaded

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Python Programming - XI. String Manipulation and Regular Expressions

  • 1. PYTHON PROGRAMMING Text Processing XI. String Manipulation and Regular Expressions Engr. Ranel O. Padon
  • 2. PYTHON PROGRAMMING TOPICS I • Introduction to Python Programming II • Python Basics III • Controlling the Program Flow IV • Program Components: Functions, Classes, Packages, and Modules V • Sequences (List and Tuples), and Dictionaries VI • Object-Based Programming: Classes and Objects VII • Customizing Classes and Operator Overloading VIII • Object-Oriented Programming: Inheritance and Polymorphism IX • Randomization Algorithms X • Exception Handling and Assertions XI • String Manipulation and Regular Expressions XII • File Handling and Processing XIII • GUI Programming Using Tkinter
  • 4. TEXT PROCESSING * used to develop text editors, word processors, page-layout soft-ware, computerized typesetting systems, and other text-processing software * used to search for patterns in text * used to validate user-inputs * used to process the contents of text files
  • 5. STRING MANIPULATION Strings are made up of Characters. Characters are made up of: Digits (0, 1, 2, …, 9) Letters (a, b, c, …, z) Symbols (@, *, #, $, %, &, …)
  • 21. REGULAR EXPRESSIONS to test if a certain string contains a day of a week, it has to test if it contains “Monday,” “Tuesday”, and so on. you will need to use the find() method seven times but, it could be solved elegantly by Regular Expressions
  • 22. REGULAR EXPRESSIONS * use string methods for simple text processing * string methods are more readable and simpler than regular expressions
  • 23. REGULAR EXPRESSION text pattern that a program uses to find substrings that will match the required pattern expression that specify a set of strings a pattern matching mechanism also known as Regex introduced in the 1950s as part of formal language theory
  • 24. REGULAR EXPRESSIONS very powerful! hundreds of code could be reduced to a one-liner elegant regular expression. used to construct compilers, interpreters, text editors, … used to search & match text patterns used to validate text data formats especially input data
  • 25. REGULAR EXPRESSIONS Popular programming languages have RegEx capabilities: Perl, JavaScript, PHP, Python, Ruby, Tcl, Java, C, C++, C#, .Net, Ruby, …
  • 26. REGEX Popular programming languages have RegEx capabilities: Perl, JavaScript, PHP, Python, Ruby, Tcl, Java, C, C++, C#, .Net, Ruby, …
  • 27. REGEX | General Concepts  Alternative  Grouping  Quantification  Anchors  Meta-characters  Character Classes
  • 28. REGEX | General Concepts  Alternative: |  Grouping: ()  Quantification: ? + * {m,n}  Anchors: ^$  Meta-characters: . [ ] [-] [^ ]  Character Classes: w d s W …
  • 29. REGEX | Alternative “ranel|ranilio” == “ranel” or “ranilio” “gray|grey” == “gray” or “grey”
  • 30. REGEX | Grouping “ran(el|ilio)” == “ranel” or “ranilio” “gr(a|e)y” == “gray” or “grey” “ra(mil|n(ny|el))” == “ramil” or “ranny” or “ranel”
  • 31. REGEX | Quantification | ? ? == zero or one of the preceding element “rani?el” == “raniel” or “ranel” “colou?r” == “colour” or “color”
  • 32. REGEX | Quantification | * * == zero or more of the preceding element “goo*gle” == “gogle” or “google” or “gooooogle” “(ha)*” == “” or “ha” or “haha” or “hahahahaha” “12*3” == “13” or “1223” or “12223”
  • 33. REGEX | Quantification | + + == one or more of the preceding element “goo+gle” == “google” or “gooogle” or “gooooogle” “(ha)+” == “ha” or “haha” or “hahahahaha” “12+3” == “123” or “1223” or “12223”
  • 34. REGEX | Quantification | {m,n} {m, n} == m to n times of the preceding element “go{2, 3}gle” == “google” or “gooogle” “6{3, 6}” == “666” or “6666” or “66666” or “666666” “5{3}” == “555” “a{2,}” == “aa” or “aaa” or “aaaa” or “aaaaa” …
  • 35. REGEX | Anchors | ^ ^ == matches the starting position within the string “^laman” == “lamang” or “lamang-loob” or “lamang-lupa” “^2013” == “2013”, “2013-12345”, “2013/1320”
  • 36. REGEX | Anchors | $ $ == matches the ending position within the string “laman$” == “halaman” or “kaalaman” “2013$” == “2013”, “777-2013”, “0933-445-2013”
  • 37. REGEX | Meta-characters | . . == matches any single character “ala.” == “ala” or “alat” or “alas” or “ala2” “1.3” == “123” or “143” or “1s3”
  • 38. REGEX | Meta-characters | [ ] [ ] == matches a single character that is contained within the brackets. “[abc]” == “a” or “b” or “c” “[aoieu]” == any vowel “[0123456789]” == any digit
  • 39. REGEX | Meta-characters | [ - ] [ - ] == matches a single character that is contained within the brackets and the specified range. “[a-c]” == “a” or “b” or “c” “[a-z]” == all alphabet letters (lowercase only) “[a-zA-Z]” == all letters (lowercase & uppercase) “[0-9]” == all digits
  • 40. REGEX | Meta-characters | [^ ] [^ ] == matches a single character that is not contained within the brackets. “[^aeiou]” == any non-vowel “[^0-9]” == any non-digit “[^abc]” == any character, but not “a”, “b”, or “c”
  • 41. REGEX | Character Classes Character classes specifies a group of characters to match in a string
  • 42. REGEX | Summary  Alternative: |  Grouping: ()  Quantification: ? + * {m,n}  Anchors: ^$  Meta-characters: . [ ] [-] [^ ]  Character Classes: w d s W …
  • 44. REGEX | Date Validation “1/3/2013” or “24/2/2020” (d{1,2}/d{1,2}/d{4})
  • 45. REGEX | Alphanumeric, -, & _ “rr2000” or “ranel_padon” or “Oblan-Padon” ([a-zA-Z0-9-_]+)
  • 46. REGEX | Numbers in 1 to 50 “1” or “50” or “14” (^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$)
  • 47. REGEX | HTML Tags “<title>” or “<strong>” or “/body” (<(/?[^>]+)>)
  • 48. PYTHON REGEX | Raw String
  • 49. PYTHON REGEX | Raw String r Two Solutions:
  • 50. PYTHON REGEX | Raw String r Raw Strings are used for enhancing readability.
  • 51. PYTHON REGEX | Raw String
  • 52. PYTHON REGEX | The re Module
  • 53. PYTHON REGEX | Samples
  • 54. PYTHON REGEX | Samples
  • 55. PYTHON REGEX | Samples
  • 56. PYTHON REGEX | Samples
  • 57. PYTHON REGEX | Samples
  • 58. PYTHON REGEX | Samples
  • 59. PYTHON REGEX | Samples
  • 60. PYTHON REGEX | Samples
  • 61. PYTHON REGEX | Samples
  • 62.
  • 63. REFERENCES  Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).  Disclaimer: Most of the images/information used here have no proper source citation, and I do not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse and reintegrate materials that I think are useful or cool, then present them in another light, form, or perspective. Moreover, the images/information here are mainly used for illustration/educational purposes only, in the spirit of openness of data, spreading light, and empowering people with knowledge. 