Tuesday 8 December 2020

Regular Expression Exercise in python

  1. Write a Python program that matches a string that has an a followed by zero or more b's
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab*',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abby
    ('Matched string is', 'abb')
    Enter a string a
    ('Matched string is', 'a')
    Enter a string b
    string didn't match
    

  2. Write a Python program that matches a string that has an a followed by one or more b's
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab+',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abby
    ('Matched string is', 'abb')
    Enter a string a
    string did'nt match
    Enter a string b
    string didn't match
    
  3. Write a Python program that matches a string that has an a followed by zero or one 'b'
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab?',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abbbbby
    ('Matched string is', 'ab')
    Enter a string a
    ('Matched string is', 'a')
    Enter a string b
    string didn't match
    
  4. Write a Python program that matches a string that has an a followed by three 'b'.
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab{3}',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abbbby
    ('Matched string is', 'abbb')
    Enter a string abb
    string did'nt match
    Enter a string bbb
    string didn't match
    

  5. Write a Python program that matches a string that has an a followed by two to three 'b'
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'ab{2,3}',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string abby
    ('Matched string is', 'abb')
    Enter a string abbbbby
    ('Matched string is', 'abbb')
    Enter a string ab
    string didn't match
    

  6. Write a Python program to find sequences of lowercase letters joined with a underscore
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'[a-z]+_[a-z]+',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string alex_lion
    ('Matched string is', 'alex_lion')
    Enter a string abbbbby
    string didn't match
    

  7. Write a Python program to find sequences of one upper case letter followed by lower case letters
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'[A-Z]{1}[a-z]+',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string Alex_lion
    ('Matched string is', 'Alex')
    Enter a string alexTheLion
    ('Matched string is', 'The')
    

  8. Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'
    
    import re
    str_check=raw_input("Enter a string")
    match=re.search(r'a.+b$',str_check)
    if match:
        print("Matched string is",match.group())
    else:
        print("string didn't match")
    
    
    
    Enter a string theappleb
    ('Matched string is', 'appleb')
    Enter a string ab
    string didn't match
    

  9. Write a Python program that matches a word containing 'z', not start or end of the word

No comments:

Post a Comment

list operations in python

How to make each term of an array repeat, with the same terms grouped together? i/p: [A,B,C] o/p: [A,A,A,B,B,B,C,C,C] Solution: >...