#Write a Python program to find the sequences of one upper case letter followed bylower case letters.
I have a string with a mix of uppercase and lowercase letters. I need want to find every lowercase letter that is surronded by 3 uppercase letters and extract it from the string. For instance ZZZaZZZ I want to extract the a in the previous string. I have written a script that is able to extract ZZZaZZZ but not the a alone. I know I need to use nested regex expressions to do this but I can not wrap my mind on how to implement this. The following is what I have: import string, re if __name__ == "__main__": #open the file eqfile = open("string.txt") gibberish = eqfile.read() eqfile.close() r = re.compile("[A-Z]{3}[a-z][A-Z]{3}") print r.findall(gibberish)EDIT: Thanks for the answers guys! I guess I should have been more specific. I need to find the lowercase letter that is surrounded by three uppercase letters that are exactly the same, such as in my example ZZZaZZZ.
The task is to find the sequence of one upper case letter followed by lower case letters using regex expression and print ‘Yes’ if it found else print ‘No’. Programimport re def search(ip_str): re_exp = '[A-Z]+[a-z]+$' if re.search(re_exp, ip_str): print('Yes, the required sequence exists!') else: print('No, the required sequence does not exists!') ip_str = input("Enter the string: ") search(ip_str)OutputExplanationIn the above python code, we have searched the pattern ‘[A-Z]+[a-z]+$’ in the given input. If the pattern is found we print ‘Yes, the required sequence exists!’ otherwise print ‘No, the required sequence does not exist!’. Hey There, Our team is currently working upon this article. As of now only ABAP Beginner Course is Live. Will update it soon.
PythonServer Side ProgrammingProgramming When it is required to find sequences of an upper case letter followed by lower case using regular expression, a method named ‘match_string’ is defined that uses the ‘search’ method to match a regular expression. Outside the method, the string is defined, and the method is called on it by passing the string. ExampleBelow is a demonstration of the same import re def match_string(my_string): pattern = '[A-Z]+[a-z]+$' if re.search(pattern, my_string): return('The string meets the required condition \n') else: return('The string doesnot meet the required condition \n') print("The string is :") string_1 = "Python" print(string_1) print(match_string(string_1)) print("The string is :") string_2 = "python" print(string_2) print(match_string(string_2)) print("The string is :") string_3 = "PythonInterpreter" print(string_3) print(match_string(string_3))OutputThe string is : Python The string meets the required condition The string is : python The string doesn’t meet the required condition The string is : PythonInterpreter The string meets the required conditionExplanation
Updated on 20-Sep-2021 10:34:15 |