s = 'hello world :)'
s.find('hel')
s = 'hello world :)'
s.find('hek')
s.find('w')
import re
s = 'hello world :)'
re.search('w', s)
re.search('m', s)
print(re.search('m', s))
char = 'm'
if re.search(char, s):
print(char, 'found')
char = 'w'
if re.search(char, s):
print(char, 'found')
re.search('^w', s)
x = 'My 2 favorite numbers are 19 and 42, 2019'
print(re.findall('[0-9]+', x))
print(re.findall('[A-C]+', x))
x = 'My 2 favorite numbers are 19 and 42, 2019'
print(re.findall('[0-9]', x))
x = 'My 2 favorite numbers are 19 and 42, 2019'
print(re.findall('[0-7]+', x))
x1 = 'My 2 favorite numbers are 19 and 42, 2019'
re.findall('[aeiou]+', x1)
x2 = 'My 2 favorite numbers are 19 and 42, 2019 at beach'
re.findall('[aeiou]+', x2)
x = 'My 2 favorite numbers are 19 and 42, 2019'
l = re.findall('[aeiou]', x)
print(l)
x = 'My 2 favorite numbers are 19 and 42, 2019'
print(re.findall('[^aeiou]', x))
x = 'My 2 favorite numbers are 19 and 42, 2019'
print(re.findall('[^aeiou]+', x))
print(re.findall('[AEIOU]+', x))
infile = open('mbox-short.txt')
import re
count = 0
for line in infile:
if re.search('^From:', line):
#print(line)
count += 1
print('count', count)
infile = open('mbox-short.txt')
import re
count = 0
for line in infile:
if re.search('^X.*:', line):
#print(line)
count += 1
print('count', count)
import re
x = 'From: Using the : character'
y = re.findall('^F.+:', x)
print(y)
import re
x = 'From: Using the : character'
y = re.findall('^F.+?:', x)
print(y)
line = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
print(line.split()[1])
import re
x = 'From msaad@iugaza.edu.ps Sat Jan 5 09:14:16 2018'
y = re.findall('\S+@\S+', x)
print(y)
import re
x1 = 'From msaad@iugaza.edu.ps Sat Jan 5 09:14:16 2018'
x2 = 'my email address is m@google.com'
y = re.findall('^From (\S+@\S+)', x1)
print(y)
y = re.findall('^From (\S+@\S+)',x2)
print(y)
import re
x = 'From msaad@iugaza.edu.ps Sat Jan 5 09:14:16 2008'
y = re.findall('@(\S+)', x)
print(y)
import re
x = 'From msaad@iugaza.edu.ps Sat Jan 5 09:14:16 2008'
y = re.findall('@([^ ]+)', x)
print(y)
import re
x = 'From msaad@iugaza.edu.ps Sat Jan 5 09:14:16 2008'
x2 = 'my email address is m@google.com'
y = re.findall('^From \S+@(\S+)', x)
print(y)
y = re.findall('^From \S+@(\S+)', x2)
print(y)
import re
x = 'From msaad@iugaza.edu.ps Sat Jan 5 09:14:16 2008'
y = re.findall('^From \S+@([^ ]+)', x)
print(y)
print(y[0])
اكتب برنامج لاستخراج سبام سكور
spam score
من ملف الرسائل الالكترونية وايجاد اعلى وادنى قيمة للسبام
import re
# open file
infile = open('mbox-short.txt')
# define empty list for spam score
spam_scores = list()
# loop on each line
for line in infile:
line = line.rstrip() # remove \n
# match
score = re.findall('^X-DSPAM-Confidence: ([0-9.]+)', line)
# print(score)
if len(score) == 0: # empty list
continue
num = float(score[0])
spam_scores.append(num)
print(spam_scores)
print(len(spam_scores))
print('Maximum:', max(spam_scores))
print('Minimum:', min(spam_scores))
اكتب برنامج لاستخراج السبام سكور والبريد الالكتروني من الترويسة من ملف الرسائل الالكترونية
import re
infile = open('mbox-short.txt').read()
scores = re.findall('^X-DSPAM-Confidence: ([0-9.]+)', infile, re.MULTILINE)
print(scores[:3]) # show sample score
emails = re.findall('^From (\S+@\S+)', infile, re.MULTILINE)
print(emails[:3]) # show sample emails
print(len(scores) == len(emails))
for email, score in zip(emails, scores):
print('email: {}\tspam score: {}'.format(email, score))
import re
x = 'We just received $10.00 for cookies.'
y = re.findall('\$[0-9.]+',x)
print(y)
import re
x = 'price = $11.99'
y = re.findall('\$[0-9.]+',x)
print(y)
اكتب برنامج لاستخراج كل الاسعار من نص
import re
text = '''
I bought a laptop for $545.00 :)
I bought milk for $1.50 only
The price of the piece is $12.60 incuding VAT
I go to the university everyday but thursday and friday
'''
money = re.findall('\$[0-9.]+', text, re.MULTILINE)
print(money)
print the line that contain a price
اكتب برنامج لطباعة الاسطر التي تحتوي على سعر
import re
text = '''
I bought a laptop for $545.00 :)
I bought milk for $1.50 only
The price of the piece is $12.60 incuding VAT
I go to the university everyday but thursday
'''
for line in text.split('\n'):
if re.search('\$[0-9.]+', line):
# print(re.search('\$[0-9.]+', line))
print(line)
import re
text = '''
I bought a laptop for $545.00 :)
I bought milk for $1.50 only
The price of the piece is $12.60 incuding VAT
I go to the university everyday but thursday
'''
for line in text.split('\n'):
if re.search('for \$[0-9.]+', line):
print(re.search('for \$[0-9.]+', line))
print(line)
import re
text = '''
I bought a laptop for $545.00 :)
I bought milk for $1.50 only
The price of the piece is $12.60 incuding VAT
I go to the university everyday but thursday
'''
for line in text.split('\n'):
if re.search('for (\$[0-9.]+)', line):
print(re.search('for (\$[0-9.]+)', line))
print(line)