w3resource

Python Regular Expression - Exercises, Practice, Solution


This resource offers a total of 290 Python Regular Expression problems for practice. It includes 58 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression.

You may read our Python regular expression tutorial before solving the following exercises.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Allowed Characters Check

Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9).

Click me to see the solution


2. a Followed by 0+ b's

Write a Python program that matches a string that has an a followed by zero or more b's.

Click me to see the solution


3. a Followed by 1+ b's

Write a Python program that matches a string that has an a followed by one or more b's.

Click me to see the solution


4. a Followed by 0 or 1 b

Write a Python program that matches a string that has an a followed by zero or one 'b'.

Click me to see the solution


5. a Followed by Exactly 3 b's

Write a Python program that matches a string that has an a followed by three 'b'.

Click me to see the solution


6. a Followed by 2 to 3 b's

Write a Python program that matches a string that has an a followed by two to three 'b'.

Click me to see the solution


7. Lowercase with Underscore

Write a Python program to find sequences of lowercase letters joined by an underscore.

Click me to see the solution


8. Uppercase Followed by Lowercase

Write a Python program to find the sequences of one upper case letter followed by lower case letters.

Click me to see the solution


9. a ... ending in b

Write a Python program that matches a string that has an 'a' followed by anything ending in 'b'.

Click me to see the solution


10. Word at Start

Write a Python program that matches a word at the beginning of a string.

Click me to see the solution


11. Word at End

Write a Python program that matches a word at the end of a string, with optional punctuation.

Click me to see the solution


12. Word Containing z

Write a Python program that matches a word containing 'z'.

Click me to see the solution


13. z in Middle

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

Click me to see the solution


14. Alphanumeric Underscore Only

Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores.

Click me to see the solution


15. Starts with Specific Number

Write a Python program that starts each string with a specific number.

Click me to see the solution


16. Remove Leading Zeros from IP

Write a Python program to remove leading zeros from an IP address.

Click me to see the solution


17. Number at End

Write a Python program to check for a number at the end of a string.

Click me to see the solution


18. Search 1-3 Digit Numbers

Write a Python program to search for numbers (0-9) of length between 1 and 3 in a given string.

"Exercises number 1, 12, 13, and 345 are important"

Click me to see the solution


19. Search Literal Strings

Write a Python program to search for literal strings within a string.
Sample text : 'The quick brown fox jumps over the lazy dog.'
Searched words : 'fox', 'dog', 'horse'

Click me to see the solution


20. Literal String with Position

Write a Python program to search for a literal string in a string and also find the location within the original string where the pattern occurs.

Sample text : 'The quick brown fox jumps over the lazy dog.'
Searched words : 'fox'

Click me to see the solution


21. Find Substrings

Write a Python program to find the substrings within a string.

Sample text :

'Python exercises, PHP exercises, C# exercises'

Pattern :

'exercises'

Note: There are two instances of exercises in the input string.

Click me to see the solution


22. Substring Occurrence and Position

Write a Python program to find the occurrence and position of substrings within a string.

Click me to see the solution


23. Swap Spaces and Underscores

Write a Python program to replace whitespaces with an underscore and vice versa.

Click me to see the solution


24. Extract Date from URL

Write a Python program to extract year, month and date from an URL.

Click me to see the solution


25. Convert Date Format

Write a Python program to convert a date of yyyy-mm-dd format to dd-mm-yyyy format.

Click me to see the solution


26. Words Starting with P

Write a Python program to match if two words from a list of words start with the letter 'P'.

Click me to see the solution


27. Extract Numbers

Write a Python program to separate and print the numbers in a given string.

Click me to see the solution


28. Words Starting with a/e

Write a Python program to find all words starting with 'a' or 'e' in a given string.

Click me to see the solution


29. Numbers and Positions

Write a Python program to separate and print the numbers and their position in a given string.

Click me to see the solution


30. Abbreviate Road

Write a Python program to abbreviate 'Road' as 'Rd.' in a given string.

Click me to see the solution


31. Replace Delimiters with Colon

Write a Python program to replace all occurrences of a space, comma, or dot with a colon.

Click me to see the solution


32. Replace Max 2 Delimiters

Write a Python program to replace maximum 2 occurrences of space, comma, or dot with a colon.

Click me to see the solution


33. Find 5-letter Words

Write a Python program to find all five-character words in a string.

Click me to see the solution


34. Find 3-5 Letter Words

Write a Python program to find all three, four, and five character words in a string.

Click me to see the solution


35. Words 4+ Letters

Write a Python program to find all words that are at least 4 characters long in a string.

Click me to see the solution


36. Camel to Snake

Write a Python program to convert a camel-case string to a snake-case string.

Click me to see the solution


37. Snake to Camel

Write a python program to convert snake-case string to camel-case string.

Click me to see the solution


38. Extract Quoted Values

Write a Python program to extract values between quotation marks of a string.

Click me to see the solution


39. Remove Extra Spaces

Write a Python program to remove multiple spaces from a string.

Click me to see the solution


40. Remove All Whitespace

Write a Python program to remove all whitespaces from a string.

Click me to see the solution


41. Remove Non-Alphanumerics

Write a Python program to remove everything except alphanumeric characters from a string.

Click me to see the solution


42. Find URLs

Write a Python program to find URLs in a string.

Click me to see the solution


43. Split into Uppercase Letters

Write a Python program to split a string into uppercase letters.

Click me to see the solution


44. Case-insensitive Replace

Write a Python program to do case-insensitive string replacement.

Click me to see the solution


45. Remove ANSI Sequences

Write a Python program to remove ANSI escape sequences from a string.

Click me to see the solution


46. Find Adverbs with Position

Write a Python program to find all adverbs and their positions in a given sentence.

Sample text : "Clearly, he has no excuse for such behavior."

Click me to see the solution


47. Split with Multiple Delimiters

Write a Python program to split a string with multiple delimiters.

Note : A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values.

Click me to see the solution


48. Check Decimal Precision

Write a Python program to check a decimal with a precision of 2.

Click me to see the solution


49. Remove Short Words

Write a Python program to remove words from a string of length between 1 and a given number.

Click me to see the solution


50. Remove Parenthesis Area

Write a Python program to remove the parenthesis area in a string.

Sample data : ["example (.com)", "w3resource", "github (.com)", "stackoverflow (.com)"]
Expected Output:
example
w3resource
github
stackoverflow
Click me to see the solution


51. Insert Spaces Before Capitals

Write a Python program to insert spaces between words starting with capital letters.

Click me to see the solution


52. Evaluate Expression

Write a Python program that reads a given expression and evaluates it.

Terms and conditions:
The expression consists of numerical values, operators and parentheses, and the ends with '='.
The operators includes +, -, *, / where, represents, addition, subtraction, multiplication and division.
When two operators have the same precedence, they are applied to left to right.
You may assume that there is no division by zero.
All calculation is performed as integers, and after the decimal point should be truncated Length of the expression will not exceed 100.
-1 ? 10 9 = intermediate results of computation = 10 9
Click me to see the solution


53. Remove Lowercase Substrings

Write a Python program to remove lowercase substrings from a given string.

Click me to see the solution


54. Concatenate Consecutive Numbers

Write a Python program to concatenate the consecutive numbers in a given string.

Original string:
Enter at 1 20 Kearny Street. The security desk can direct you to floor 1 6. Please have your identification ready.
After concatenating the consecutive numbers in the said string:
Enter at 120 Kearny Street. The security desk can direct you to floor 16. Please have your identification ready.
Click me to see the solution


55. Convert to Snake Case

Write a Python program to convert a given string to snake case.

Sample Output:
java-script
gd-script
btw...-what-*-do*-you-call-that-naming-style?-snake-case?
Click me to see the solution


56. Longest Iterable

Write a Python program that takes any number of iterable objects or objects with a length property and returns the longest one.

Sample Output:
Orange
[1, 2, 3, 4, 5]
Java
Python
Click me to see the solution


57. Word Starts & Ends with Vowel

Write a Python program that checks whether a word starts and ends with a vowel in a given string. Return true if a word matches the condition; otherwise, return false.

Sample Data:
("Red Orange White") -> True
("Red White Black") -> False
("abcd dkise eosksu") -> True
Click me to see the solution


58. Consecutive Vowel Check

Write a Python program that takes a string with some words. For two consecutive words in the said string, check whether the first word ends with a vowel and the next word begins with a vowel. If the program meets the condition, return true, otherwise false. Only one space is allowed between the words.

Sample Data:
("These exercises can be used for practice.") -> True
("Following exercises should be removed for practice.") -> False
("I use these stories in my classroom.") -> True
Click me to see the solution


Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.