How to Write a Case-Insensitive Python Regular Expression Without re.compile?Introduction:In this tutorial we are learning to write the case insensitive Python regular expression or regex without re.compile method. Regular expressions or regex are useful tools for pattern matching and searching in the strings. By default, the regular expression patterns are case sensitive; that is, they only match characters with the upper or lower case specified in the pattern. But sometimes you can do a random match where the pattern will match the characters regardless of their case. The re.IGNORECASE is a flag that allows a regular expression to match the given string in an insensitive manner. For example, an expression like [A-Z] also matches the lowercase letters. It usually becomes an optional argument to the re.compile() function. Let us take an example where the user is asked to enter their name and we need to validate it by using the RegEx. The format for writing your name is given as below:
Example: Here, we give an example of writing your name in the input and the expected output. The example is given below - Because we are using the IGNORECASE flag, the first character of the first name, middle name, and last name may or may not be uppercase. Program Code 1: Here we give a program code to write the case insensitive Python regular expression or regex without re.compile. The code is given below - Output: Now we run the above code and find the case insensitive Python regular expression without re.compile. The result is given below - <re.Match object; span=(0, 4), match='MaRk'> <re.Match object; span=(0, 4), match='MaRk'> xxxxmeat Program Code 2: Here we give a program code to write a case insensitive Python regular expression with re.compile. This program is checking to validate the name by using the IGNORECASE in RegEx. The code is given below - Output: Now, we run the above code and check whether the given name is validated or not by using the IGNORECASE in RegEx. Here, we checked some names, and the validation result is given below - It is not a valid name It is not a valid name It is a valid name It is a valid name It is not a valid name It is not a valid name Conclusion:In this tutorial, we learn to write a case-insensitive Python regular expression without re.compile. The re.IGNORECASE is a flag that allows a regular expression to match the given string in an insensitive manner. Here, we are using the IGNORECASE flag. The first character of the first name, middle name, and last name may or may not be uppercase. We learn some program code to write a case-insensitive Python regular expression. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India