9/10

 avatar
user_7840863
plain_text
2 months ago
6.8 kB
1
Indexable
Never
import re
def main():
    s= "Hello everyone. My name is Alice. I'm 25 years old. I live in south of London. My favorite colors are red and blue. I have got 2 brothers and 3 sisters."
    c=0
    while c!=7:
        print("***********************MENU*********************************")
        print("1- Search_pattern ")
        print("2-Search_word ")
        print("3-Find All_words ")
        print("4-Find All_numbers ")
        print("5-Replace ")
        print("6-Split ")
        print("7-Exit ")
        print("************************************************************ ")
        c=int(input("Please enter your choice: "))
        if c==1:
            r=re.search('([st]h)', s)
            if r:
                print(r.group(1))
            else :
                print("No Result Found")
        
        elif c==2:    
            r=re.search('(A[a-z]+e)', s)
            if r:
                print(r.group(1))
            else :
                print("No Result Found")
        elif c==3:
            r=re.findall('on+[a-z]*', s)
            if len(r)!=0:
               print(r)
               
            else :
                print("No Result Found")
        elif c==4:
            r=re.findall('[+\-]?[0-9]+', s)
            if len(r)!=0:
               print(r)
               
            else :
                print("No Result Found")
            
        elif c==5:
            print(s)
            if(re.search('and', s)):
                se=re.sub('and','&',s)
            print(se)
        elif c==6:
            print('',end=' ')
            r=re.split('\.',s)
            for i in r:
                print (i)
        elif c==7:
            print('Good Bye')
        else:
            print('wrong insertion')
main()
    ******************************************************************************************************************

import re

def main():
    # Define the given string
    Str = "There are 6 departments in the CIT Faculty at JUST: CPE, CS, CIS, NES, CS, SE"

    while True:
        # Print the menu
        print("1-Search\n2-Replace\n3-Find All\n4-Split\n9-Exit")

        # Get the user's choice
        choice = input("Enter your choice: ")

        if choice == '1':
            search_department(Str)
        elif choice == '2':
            replace_number(Str)
        elif choice == '3':
            find_all_departments(Str)
        elif choice == '4':
            split_string(Str)
        elif choice == '9':
            print("Exiting...")
            break
        else:
            print("Invalid choice. Please try again.")

def search_department(Str):
    pattern = r'\b[M]\w*\b'
    result = re.search(pattern, Str)
    if result:
        print(f"Found: {result.group()}")
    else:
        print("Not found")

def replace_number(Str):
    pattern = r'\b6\b'
    print(f"Original String: {Str}")
    new_str = re.sub(pattern, 'six', Str)
    print(f"Modified String: {new_str}")

def find_all_departments(Str):
    pattern = r'\bC\w*E\b'
    result = re.findall(pattern, Str)
    if result:
        print("Found:")
        for department in result:
            print(department)
    else:
        print("Not found")

def split_string(Str):
    result = re.split(r'\:|\,', Str)
    print("\n".join(result))

if __name__ == "__main__":
    main()
***********************************************************************************************************************
import re
def main():
    
    
    c=0
    while c!=8:
        print("*************************MENU******************************\n\n" )
        print("""1. Display the full name of all students.\n
2. Display the first name and the ID of all students.\n
3. Display the first name and the gender of all students.\n
4. Display the first name and the birth date of all students.\n
5. Display the full name of students whose born on a specific year.\n
6. Display the first name and the courses of all students.\n
7. Display the full name of students whose name start with a specific letter.\n
8. Exit\n""")
        try:
            c=int(input("Please enter your choice:"))
        except ValueError:
            c=-1
            
        if c==1:
            file = open("students.txt")
            print()
            for l in file:
                s=re.match("([A-Z][a-z]+\s)+",l)
                if s:
                    print(s.group())
            file.close()
        elif c==2:
            file = open("students.txt")
            print()
            for l in file:
                n=re.match("[A-Z][a-z]+",l)
                i=re.search("[0-9]+",l)
                if (n and i):
                    print(n.group(),"        ",i.group())
            file.close()
            
        elif c==3:
            file = open("students.txt")
            print()
            for l in file:
                n=re.match("[A-Z][a-z]+",l)
                g=re.search("\s[a-z]+",l)
                if (n and g):
                    print(n.group(),"        ",g.group())
            file.close()
            
        elif c==4:
            file = open("students.txt")
            print()
            for l in file:
                n=re.match("[A-Z][a-z]+",l)
                b=re.search("\d+-\d+-\d+",l)
                if (n and b):
                    print(n.group(),"        ",b.group())
            file.close()
        elif c==5:
            file = open("students.txt")
            print()
            try:
                y=(int(input("Enter a year :")))
            except ValueError:
                y=-1
            for l in file:
                if(re.search("-"+str(y)+"\s",l)):
                    n=re.match("([A-Z][a-z]+\s)+",l)
                    if n:
                        print(n.group(),"        ",y)
                
            file.close()    
        elif c==6:
            file = open("students.txt")
            print()
            for l in file:
                n=re.match("[A-Z][a-z]+",l)
                co=re.search("(\w+\.)+",l)
                if (n and co):
                    print(n.group(),"        ",co.group())
            file.close()
        elif c==7:
            file = open("students.txt")
            print()
            letter=((input("Enter a letter :")))
            for l in file:
                if(re.match(letter.upper(),l)):
                    n=re.match("([A-Z][a-z]+\s)+",l)
                    if n:
                        print(n.group())
            file.close()
        elif c==8:
            print("Goodbye")
        else:
            print("invalid choice, choose again please")
main()
Leave a Comment