regexp
#! /usr/bin/env python3
import re
from string import punctuation
my_folder = "c:\desktop\notes"
print(my_folder)
my_folder = r"c:\desktop\notes"
print(my_folder)
result_search = re.search("pattern", r"string to contain the pattern")
print(result_search)
result_search_2 = re.search("pattern", r"the phrase to find isn't in this string")
print(result_search_2)
string = r"sara was able to help me find the items I needed quickly"
new_string = re.sub("sara", "sarah", string)
print(new_string)
print()
print("Customer reviews with sara/sarah")
customer_reviews = ['julie loves ice cream, books, and kittens. ',
'sam was a great help to me in the store.',
'the cashier was very rude to me, I think her name was eleanor.',
'amazing work from sadeen!',
'sarah was able to help me find the items i needed quickly.',
'lucy is such a great addition to the team.',
'great service from sara she found me what i wanted.']
sarahs_reviews = []
pattern_to_find = r"sarah?"
for string in customer_reviews:
if (re.search(pattern_to_find, string)):
sarahs_reviews.append(string)
print(sarahs_reviews)
a_reviews = []
pattern_to_find = r"^a"
for string in customer_reviews:
if (re.search(pattern_to_find, string)):
a_reviews.append(string)
print("Customer reviews that start with a")
print(a_reviews)
print()
print("Customer reviews that end with y")
y_reviews = []
pattern_to_find = r"y$"
for string in customer_reviews:
if (re.search(pattern_to_find, string)):
y_reviews.append(string)
print(y_reviews)
print()
print("Customer reviews that have needed or wanted")
needwant_reviews = []
pattern_to_find = r"(need|want)ed"
for string in customer_reviews:
if (re.search(pattern_to_find, string)):
needwant_reviews.append(string)
print(needwant_reviews)
print()
print("No punctuation")
no_punct_reviews = []
pattern_to_find = r"[^\w\s]"
for string in customer_reviews:
no_punct_string = re.sub(pattern_to_find, "", string)
no_punct_reviews.append(no_punct_string)
print()
print('Customer Reviews: ', customer_reviews)
print('No Punct Review: ', no_punct_reviews)
C:\Users\Win10x64i7\PycharmProjects\regexp\.venv\Scripts\python.exe C:\Users\Win10x64i7\PycharmProjects\regexp\regexp.py
C:\Users\Win10x64i7\PycharmProjects\regexp\regexp.py:6: SyntaxWarning: invalid escape sequence ‘\d’
my_folder = “c:\desktop\notes”
c:\desktop
otes
c:\desktop\notes
<re.Match object; span=(22, 29), match=’pattern’>
None
sarah was able to help me find the items I needed quickly
Customer reviews with sara/sarah
[‘sarah was able to help me find the items i needed quickly.’, ‘great service from sara she found me what i wanted.’]
Customer reviews that start with a
[‘amazing work from sadeen!’]
Customer reviews that end with y
[]
Customer reviews that have needed or wanted
[‘sarah was able to help me find the items i needed quickly.’, ‘great service from sara she found me what i wanted.’]
No punctuation
Customer Reviews: [‘julie loves ice cream, books, and kittens. ‘, ‘sam was a great help to me in the store.’, ‘the cashier was very rude to me, I think her name was eleanor.’, ‘amazing work from sadeen!’, ‘sarah was able to help me find the items i needed quickly.’, ‘lucy is such a great addition to the team.’, ‘great service from sara she found me what i wanted.’]
No Punct Review: [‘julie loves ice cream books and kittens ‘, ‘sam was a great help to me in the store’, ‘the cashier was very rude to me I think her name was eleanor’, ‘amazing work from sadeen’, ‘sarah was able to help me find the items i needed quickly’, ‘lucy is such a great addition to the team’, ‘great service from sara she found me what i wanted’]
Process finished with exit code 0