
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python regex to find sequences of one upper case letter followed by lower case letters
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.
Example
Below 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))
Output
The 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 condition
Explanation
The required packages are imported.
A method named ‘match_string’ is defined, that takes a string as a parameter.
It uses the ‘search’ method to check if the specific regular expression was found in the string or not.
Outside the method, a string is defined, and is displayed on the console.
The method is called by passing this string as a parameter.
The output is displayed on the console.
- Related Questions & Answers
- MySQL Query to change lower case to upper case?
- How to convert Lower case to Upper Case using C#?
- How to convert Upper case to Lower Case using C#?
- Moving all Upper case letters to the end of the String using Java RegEx
- Python program to count upper and lower case characters in a given string.
- Python program to count upper and lower case characters without using inbuilt functions
- How to check if a string contains only upper case letters in Python?
- How to generate random strings with upper case letters and digits in Python?
- C program to convert upper case to lower and vice versa by using string concepts
- How to check if a string contains only lower case letters in Python?
- Convert mixed case string to lower case in JavaScript
- Count upper and lower case characters without using inbuilt functions in Python program
- Java Program to check whether the entered character a digit, white space, lower case or upper case character
- Return String by capitalizing first letter of each word and rest in lower case with JavaScript?
- Converting to upper case in Java.
Advertisements