Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Regex Methods – A Short Overview

#1
Python Regex Methods – A Short Overview

Python’s re module comes with a number of regular expression methods that help you achieve more with less.



Think of those methods as the framework connecting regular expressions with the Python programming language. Every programming language comes with its own way of handling regular expressions. For example, the Perl programming language has many built-in mechanisms for regular expressions—you don’t need to import a regular expression library—while the Java programming language provides regular expressions only within a library. This is also the approach of Python.

These are the most important regular expression methods of Python’s re module:

  • re.findall(pattern, string): Checks if the string matches the pattern and returns all occurrences of the matched pattern as a list of strings.
  • re.search(pattern, string): Checks if the string matches the regex pattern and returns only the first match as a match object. The match object is just that: an object that stores meta information about the match such as the matching position and the matched substring.
  • re.match(pattern, string): Checks if any string prefix matches the regex pattern and returns a match object.
  • re.fullmatch(pattern, string): Checks if the whole string matches the regex pattern and returns a match object.
  • re.compile(pattern): Creates a regular expression object from the pattern to speed up the matching if you want to use the regex pattern multiple times.
  • re.split(pattern, string): Splits the string wherever the pattern regex matches and returns a list of strings. For example, you can split a string into a list of words by using whitespace characters as separators.
  • re.sub(pattern, repl, string): Replaces (substitutes) the first occurrence of the regex pattern with the replacement string repl and return a new string.

Example: Let’s have a look at some examples of all the above functions:

import re text = '''
LADY CAPULET Alack the day, she's dead, she's dead, she's dead! CAPULET Ha! let me see her: out, alas! she's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. Nurse O lamentable day! ''' print(re.findall('she', text)) '''
Finds the pattern 'she' four times in the text: ['she', 'she', 'she', 'she'] ''' print(re.search('she', text)) '''
Finds the first match of 'she' in the text: <re.Match object; span=(34, 37), match='she'> The match object contains important information
such as the matched position. ''' print(re.match('she', text)) '''
Tries to match any string prefix -- but nothing found: None ''' print(re.fullmatch('she', text)) '''
Fails to match the whole string with the pattern 'she': None ''' print(re.split('\n', text)) '''
Splits the whole string on the new line delimiter '\n': ['', 'LADY CAPULET', '', " Alack the day, she's dead, she's dead, she's dead!", '', 'CAPULET', '', " Ha! let me see her: out, alas! she's cold:", ' Her blood is settled, and her joints are stiff;', ' Life and these lips have long been separated:', ' Death lies on her like an untimely frost', ' Upon the sweetest flower of all the field.', '', 'Nurse', '', ' O lamentable day!', ''] ''' print(re.sub('she', 'he', text)) '''
Replaces all occurrences of 'she' with 'he': LADY CAPULET Alack the day, he's dead, he's dead, he's dead! CAPULET Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. Nurse O lamentable day! '''

Now, you know the most important regular expression functions. You know how to apply regular expressions to strings. But you don’t know how to write your regex patterns in the first place. Let’s dive into regular expressions and fix this once and for all! 

Where to Go From Here


You’ve learned a quick overview of the Python regular expression methods. These are the basis of all advanced regex concepts in Python.

Want to learn more about regular expressions and acquire the Python regex superpower? Read my 17,000 word tutorial on the Finxter blog!

Need to brush up your Python skills? Join the free Finxter Computer Science Email Academy!



https://www.sickgaming.net/blog/2020/02/...-overview/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How To Format A String That Contains Curly Braces In Python? xSicKxBot 0 3 09-25-2020, 07:51 PM
Last Post: xSicKxBot
  [Tut] Python One Line Dictionary xSicKxBot 0 14 09-21-2020, 05:58 PM
Last Post: xSicKxBot
  [Tut] Python Import Error (ModuleNotFoundError) xSicKxBot 0 10 09-20-2020, 04:15 PM
Last Post: xSicKxBot
  [Tut] Python Unicode Encode Error xSicKxBot 0 11 09-19-2020, 03:25 PM
Last Post: xSicKxBot
  [Tut] How to Solve Python “TypeError: ‘int’ object is not iterable”? xSicKxBot 0 12 09-18-2020, 01:49 PM
Last Post: xSicKxBot
  [Tut] Python One Line Generator xSicKxBot 0 11 09-18-2020, 12:29 AM
Last Post: xSicKxBot
  [Tut] Python Keyboard Errors xSicKxBot 0 15 09-14-2020, 08:39 PM
Last Post: xSicKxBot
  [Tut] Replacements For Switch Statement In Python? xSicKxBot 0 19 09-13-2020, 04:31 PM
Last Post: xSicKxBot
  [Tut] Python raw_input() vs input() xSicKxBot 0 24 09-13-2020, 03:48 AM
Last Post: xSicKxBot
  [Tut] What Is Python Output Buffering and How to Disable It? xSicKxBot 0 20 09-12-2020, 02:00 PM
Last Post: xSicKxBot

Forum Jump:

Become a Patron!
[-]
Upcoming Events

[-]
Latest Threads
Microsoft - Find out about storage optio...
Last Post: xSicKxBot
Today 01:12 AM
» Replies: 0
» Views: 3
News - Deathsmiles Collection Brings Mor...
Last Post: xSicKxBot
Today 01:12 AM
» Replies: 0
» Views: 3
News - Talking Point: What Are You Playi...
Last Post: xSicKxBot
Today 01:12 AM
» Replies: 0
» Views: 3
News - Fortnite Is Getting Special Chall...
Last Post: xSicKxBot
Today 01:12 AM
» Replies: 0
» Views: 3
[Tut] List Changes After Assignment — Ho...
Last Post: xSicKxBot
Today 12:14 AM
» Replies: 0
» Views: 3
(Indie Deal) Castlevania Collection, Gho...
Last Post: xSicKxBot
Today 12:13 AM
» Replies: 0
» Views: 4
Epic Games Have Acquired Super Awesome
Last Post: xSicKxBot
Yesterday 07:02 PM
» Replies: 0
» Views: 4
News - Mojang To Reveal Next Big Minecra...
Last Post: xSicKxBot
Yesterday 07:01 PM
» Replies: 0
» Views: 3
News - Fortnite Players Love The Lonely ...
Last Post: xSicKxBot
Yesterday 07:01 PM
» Replies: 0
» Views: 3
(Indie Deal) Borderlands 3 (-58%) & Kona...
Last Post: xSicKxBot
Yesterday 12:08 PM
» Replies: 0
» Views: 6

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020