0% found this document useful (0 votes)
6 views6 pages

Understanding Regular Expressions in Python

The document provides an overview of regular expressions, including various patterns and modifiers used for matching strings. It includes examples of functions like re.search, re.split, re.findall, re.sub, and re.subn, demonstrating how to manipulate and search through strings using these methods. The document also explains how to perform substitutions and count the number of replacements made.

Uploaded by

anilkumar1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Understanding Regular Expressions in Python

The document provides an overview of regular expressions, including various patterns and modifiers used for matching strings. It includes examples of functions like re.search, re.split, re.findall, re.sub, and re.subn, demonstrating how to manipulate and search through strings using these methods. The document also explains how to perform substitutions and count the number of replacements made.

Uploaded by

anilkumar1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Regular Expression

1
Regular Expressions
"""identifiers
\w any character \W anything but character
\s for whitespace \S anything but whitespace
\d number \D anything but number
Modifiers
. any character, * 0 or more, + 1 or more, ? 0 or one
$ end of the string, ^ begining of the string, [] variance
{} range, () group """
import re
ex_s="""jessica is 9 year old and her email id is jessi@[Link]
and jes@[Link], 234 dwnstreet"""
mt=[Link](r'ssi', ex_s)
print(mt)
print([Link]())
mt=[Link](r'\d{3}\s\w+', ex_s)
print([Link]())
mt=[Link](r'[\w.]+@[\w.]+', ex_s)
print([Link]())

2
Regular Expression…
import re
#[Link]() returns list and work similar to string with added
functionalities
print([Link](r"\s*", "here are some words"))
print([Link](r"s*", "here are some words"))
print([Link](r"(s*)", "here are some words"))
print([Link](r"[a-fA-F]", r"adaslkf
dewebekwcbeffertyuyujinHHklhjkFFFjloo[o"))
print([Link](r'\d\s\D*\d?', 'BYL93 LIG Barra-2 kanpur'))
match = [Link](r'([\w.]+)@([\w.]+)', 'my email is
[Link]@[Link] and chandra_sumit@[Link]')
print(match)
"""
* 0 or more
+ 0 or more
? 0 or One
{5} exactly five
{1,5} one to five
\w alphanumeric
"""

3
Regular Expressions…
 [Link]. In regular expressions, sub stands for substitution. The
[Link] method applies a method to all matches.
 [Link] can replace a pattern match with a simple string.

import re

# An example string.
v = "running eating reading"

# Replace words starting with "r" and ending in "ing"


# ... with a new string.
v = [Link](r"r.*?ing", "ring", v)
print(v)

4
Regular Expression…
 import re

 def multiply(m):
 # Convert group 0 to an integer.
 v = int([Link](0))
 # Multiply integer by 2.
 # ... Convert back into string and return it.
 return str(v * 2)

 # Use pattern of 1 or more digits.


 # ... Use multiply method as second argument.
 result = [Link]("\d+", multiply, "10 20 30 40 50")
 print(result)

5
Regular Expressions
 Subn. Usually [Link]() is sufficient. But another option exists. The
[Link] method has an extra feature. It returns a tuple with a
count of substitutions in the second element.
import re
def add(m):
# Convert.
v = int([Link](0))
# Add 2.
return str(v + 1)
# Call [Link].
result = [Link]("\d+", add, "1 2 3 4 5")
print("Result string:", result[0])
print("Number of substitutions:", result[1])
O/p
 Result string: 2 3 4 5 6
 Number of substitutions: 5 6

You might also like