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] Python Character Set [Regex Tutorial] xSicKxBot 0 0 22 minutes ago
Last Post: xSicKxBot
  [Tut] Python AND Operator On Two Objects or Lists [What’s The Result?] xSicKxBot 0 5 02-17-2020, 07:47 PM
Last Post: xSicKxBot
  [Tut] Python Regex Syntax [2-Minute Primer] xSicKxBot 0 8 02-15-2020, 08:32 PM
Last Post: xSicKxBot
  [Tut] Python cProfile – 7 Strategies to Speed Up Your App xSicKxBot 0 10 02-11-2020, 09:35 PM
Last Post: xSicKxBot
  [Tut] Python cProfile – A Helpful Guide with Prime Example xSicKxBot 0 14 02-10-2020, 12:38 PM
Last Post: xSicKxBot
  [Tut] Python Regex And Operator [Tutorial + Video] xSicKxBot 0 25 02-07-2020, 09:09 AM
Last Post: xSicKxBot
  [Tut] Python Regex Or – A Simple Illustrated Guide xSicKxBot 0 19 02-06-2020, 06:03 PM
Last Post: xSicKxBot
  [Tut] Python Regex – How to Match the Start of Line (^) and End of Line ($) xSicKxBot 0 34 02-02-2020, 09:38 AM
Last Post: xSicKxBot
  [Tut] The Python Re Plus (+) Symbol in Regular Expressions xSicKxBot 0 22 02-01-2020, 07:38 AM
Last Post: xSicKxBot
  [Tut] Python Re * – The Asterisk Quantifier for Regular Expressions xSicKxBot 0 23 01-31-2020, 03:39 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Upcoming Events

[-]
Discord

[-]
Latest Threads
[Tut] Python Character Set [Regex Tutori...
Last Post: xSicKxBot
Today 12:12 AM
» Replies: 0
» Views: 0
(Indie Deal) Fight for Sanity Bundle & G...
Last Post: xSicKxBot
Today 12:12 AM
» Replies: 0
» Views: 0
Clockwork GameShell Review And Godot Tut...
Last Post: xSicKxBot
Today 12:12 AM
» Replies: 0
» Views: 0
Mobile - The Weekender: iOS Beta Edition
Last Post: xSicKxBot
Today 12:11 AM
» Replies: 0
» Views: 0
Microsoft - Microsoft Connected Vehicle ...
Last Post: xSicKxBot
Today 12:11 AM
» Replies: 0
» Views: 0
News - This Week At Bungie – 1/23/2020
Last Post: xSicKxBot
Today 12:10 AM
» Replies: 0
» Views: 0
News - Surprise! First-Person Shooter Wa...
Last Post: xSicKxBot
Today 12:09 AM
» Replies: 0
» Views: 0
News - Overwatch And Diablo Animated Ser...
Last Post: xSicKxBot
Today 12:09 AM
» Replies: 0
» Views: 0
News - Teaser Image Of Sword & Shield's ...
Last Post: xSicKxBot
Today 12:08 AM
» Replies: 0
» Views: 0
News - Blog: A firsthand account of coro...
Last Post: xSicKxBot
Yesterday 09:20 PM
» Replies: 0
» Views: 4

[-]
Twitter

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

Copyright © SickGaming.net 2012-2019