Untitled

 avatar
unknown
python
a year ago
3.6 kB
5
Indexable
'''
import re
s ="There are twelve months of the year: January , February , March , April , May , June , July , August , September , October , November , December ."
flag=True
while flag:
    print('***********************MENU*********************************')
    print('1-Search\n2-Find All\n3-Replace\n4-Split\n5-Exit')
    print('************************************************************')
    c=int(input('Please enter your choice:'))
    if c==1:
        result=re.search('[A]\w+', s)  
        if result:
            print(result.group())
        else:
            print('No Result Found')    
    elif c==2:
        result=re.findall('(\w+er\s)[\,\.]', s)
        if result:
            print(result)
        else:
            print('No Result Found')
    elif c==3:
        print('**********Before Replace***************')
        print(s)
        result=re.sub('twelve', '12',s) 
        print('**********Aftar Replace***************')
        print(result)    
    elif c==4:
        result=re.split(',',s)  
        for i in result:
            print(i) 
            
    elif c==5:
        print('Good Bye\n>>>')
        flag=False
    else:
        print('Wrong choice')            
'''        
import re

flag=True
while flag:
    f=open("students.txt")
    print('*************************MENU*********************************')
    print('1. Display the full name of all students.\n2. Display the full name and the ID of all students.\n3. Display the full name and the gender of all students.\n4. Display the full name and the birth date of all students.\n5. Display the full name of students whose born on a specific year.\n6. Display the full name and the courses of all students.\n7. Display the full name of students whose name start with a specific letter.\n8. Exit')
    c1=int(input('Please enter your choice:'))
    if c1==1:
        for line in f:
            r=re.search('[A-Z][a-z]+\s[A-Z][a-z]+\s', line)
            print(r.group())
    elif c1==2:
        for line in f:
            r1=re.search('[A-Z][a-z]+\s[A-Z][a-z]+\s', line)
            r2=re.search('[0-9]+\s', line)
            print(r1.group(),'\t',r2.group())
         
    elif c1==3:
        for line in f:
            r1=re.search('[A-Z][a-z]+\s[A-Z][a-z]+\s', line)
            r2=re.search('female|male\s', line)
            print(r1.group(),'\t',r2.group())  
    elif c1==4:
        for line in f:
            r1=re.search('[A-Z][a-z]+\s[A-Z][a-z]+\s', line)
            r2=re.search('[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}', line)
            print(r1.group(),'\t',r2.group())        
    elif c1==5:
        y=int(input('Enter a year :'))
        n=str(y)
        for line in f:
            r1=re.search('[A-Z][a-z]+\s[A-Z][a-z]+\s', line)
            r2=re.search('[0-9]{1,2}-[0-9]{1,2}-'+n, line)
            if r2:
                print(r1.group())   
    elif c1==6:
        for line in f:
            r1=re.search('[A-Z][a-z]+\s[A-Z][a-z]+\s', line)
            r2=re.search('([A-Za-z0-9]+\.)+', line)
            print(r1.group(),'\t',r2.group()) 
    elif c1==7:
        n=input('Enter a letter:')
        for line in f:
            r1=re.search('[A-Z][a-z]+\s[A-Z][a-z]+\s', line)
            r2=re.search('[A-Z][a-z]+\s', line)
            r3=re.search(n+'[a-z]+\s', r2.group())
            if r3:
                print(r1.group())                           
    elif c1==8:
        print('Good Bye\n>>>')
        f.close()
        flag=False
    else:
        print('Wrong choice') 
    
         
Editor is loading...
Leave a Comment