How to match any one uppercase character in python using Regular Expression?


There are two ways to match any one uppercase character in python using Regular Expression. One is the general method and other is using the regular expression.

Using Regular Expression

A regular expression is a series of characters that allows you to discover a string or a set of strings using a search pattern. Regular expressions are sometimes known as RegEx. In Python, the re module is used to work with regular expressions.

In this article we will match the uppercase vowel in python using regular expression. To achieve this, we use r'[A,E,I,O,U]' regular expression. Here, we have used a set of uppercase vowels [A,E,I,O,U] as we need to match them. In the following code, lets us see how to match lowercase vowels.

Example

In the following code, let us see how to match uppercase vowels using regular expression. We begin by importing regular expression module.

import re

Then, we have used findall() function which is imported from the re module.

import re string = 'TutRoIals POinT Is A GreAt PlaTfOrm to lEarN' res = re.findall(r'[A,E,I,O,U]', string) print (res)

The re.findall() function returns a list containing all matches.

Output

On executing the above program, the list of uppercase vowels is printed as output.

['I', 'O', 'I', 'A', 'A', 'O', 'E']

Using General method

In the following code, we have matched all the uppercase vowels from the string 'TutRoIals POinT Is A GreAt PlaTfOrm to lEarN'. Here we did not use regular expression to match.

Example

The following example is a program which shows the matching of any one uppercase character in python using the general method.

string='TutRoIals POinT Is A GreAt PlaTfOrm to lEarN' lst = [] for i in string: if (i=='A' or i=='E' or i=='I' or i=='O' or i=='U'): lst.append(i) print(lst)

Output

On executing the above program, the list of uppercase vowels is printed as output.

['I', 'O', 'I', 'A', 'A', 'O', 'E']

Updated on: 08-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements