Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python - Regular Expressions - Code

The document provides examples of how to use regular expression patterns in Python including character sets, special sequences, wildcards, quantifiers, grouping and alternation. Each pattern is accompanied by a short Python code snippet to search and extract matches from a sample string.

Uploaded by

Eswar Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python - Regular Expressions - Code

The document provides examples of how to use regular expression patterns in Python including character sets, special sequences, wildcards, quantifiers, grouping and alternation. Each pattern is accompanied by a short Python code snippet to search and extract matches from a sample string.

Uploaded by

Eswar Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

----------------

[] A set of characters "[a-m]"


\ Signals a special sequence (can also be used to escape special characters) "\d"
. Any character (except newline character) "he..o"
^ Starts with "^hello"
$ Ends with "world$"
* Zero or more occurrences "aix*"
+ One or more occurrences "aix+"
{} Exactly the specified number of occurrences"al{2}"
| Either or "falls|stays"
() Capture and group

----------------------------------------------------
[] A set of characters
-------------------------

import re

str = "The rain in Spain"

#Find all lower case characters alphabetically between "a" and "m":

x = re.findall("[a-m]", str)
print(x)

------------------------
\ Signals a special sequence
------------------------------
import re

str = "That will be 59 dollars"

#Find all digit characters:

x = re.findall("\d", str)
print(x)

-------------------------
. Any character (except newline character)
----------------------------------------
import re

str = "hello world"

#Search for a sequence that starts with "he", followed by two (any) characters, and an "o":

x = re.findall("he..o", str)
print(x)

-----------------------
^ Starts with
-----------------
import re

str = "hello world"

#Check if the string starts with 'hello':

x = re.findall("^hello", str)
if (x):
print("Yes, the string starts with 'hello'")
else:
print("No match")

---------------------------
$ Ends with
-------------
import re

str = "hello world"

#Check if the string ends with 'world':

x = re.findall("world$", str)
if (x):
print("Yes, the string ends with 'world'")
else:
print("No match")
----------------------------
* Zero or more occurrences
----------------------------
import re
str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains "ai" followed by 0 or more "p" characters:

x = re.findall("aip*", str)
print(x)

if (x):
print("Yes, there is at least one match!")
else:
print("No match")
-----------------------------------------
+ One or more occurrences
----------------------------------
import re

str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains "ai" followed by 1 or more "p" characters:

x = re.findall("aip+", str)

print(x)

if (x):
print("Yes, there is at least one match!")
else:
print("No match")
-------------------------------------------------
{} Exactly the specified number of occurrences
--------------------------------------------------
import re

str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains "a" followed by exactly two "l" characters:

x = re.findall("al{2}", str)

print(x)

if (x):
print("Yes, there is at least one match!")
else:
print("No match")
-----------------------------------------------
| Either or
-------------------
import re

str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains either "falls" or "stays":

x = re.findall("falls|stays", str)

print(x)

if (x):
print("Yes, there is at least one match!")
else:
print("No match")
-----------------------------

-------------------

You might also like