
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Regular Expression Grouping in Python
To extract decimal numbers from a string in Python, regular expressions are used. A regular expression is a group of characters that allows you to use a search pattern to find a string or a set of strings. RegEx is another name for regular expressions.
The re module in Python is used to work with regular expressions. In this article, we will learn how to extract decimal numbers from a string in Python using regular expressions.
We use the following regular expression in Python to get non-digit characters from a string -
\d+\.\d+
Where,
-
\d returns a match where the string contains digits (numbers from 0 9)
-
+ implies zero or more occurrences of characters.
-
\ signals a special sequence (can also be used to escape special characters).
-
. is any character (except the newline character).
How to Use Grouping in Python?
We can use the 're module' in Python for grouping. Here is how you can import it -
import re
For creating groups you have to use () in your regex pattern. Built-in functions we can use for groupings are group() and groups(). Here group() method is used to return the whole match or a specific group and groups() method is used to return all the captured groups as a tuple.
The syntax for using group() and groups() methods is as follows -
re.group([group1, ...]) re.groups([group1, ...])
Example 1
Here is an example of how to use group() and groups() methods in Python. So we have used group() method to extract first name and last name from the string.
import re text = "Name: Ankit Sharma" match = re.search(r"Name: (\w+) (\w+)", text) # First name print(match.group(1)) # Last name print(match.group(2))
In the above example, we have used group() method to extract first name and last name from the string. After running the above code snippet, the below output is obtained -
Ankit Sharma
Example 2
Here is another example of how to use group() and groups() methods in Python. So our program will group the username, domain and extension of an email. Each part is captured using () and returned as a tuple.
import re email = "Email: [email protected]" match = re.search(r"Email: (\w+)@(\w+)\.(\w+)", email) # All parts of the email print(match.groups())
In the above example, we have used groups() method to extract all parts of the email. The above code will return the result as follows -
('user', 'example', 'com')
Example 3
Here is another example of how to use the groups() and group() functions in Python. So, our program will group the product name and price from a string. Each component is stored using () and returned as a tuple.
import re text = "Product: Mobile, Price: $500" match = re.search(r"Product: (\w+), Price: \$(\d+)", text) print(f"Item: {match.group(1)}, Price: {match.group(2)}")
After running the above code snippet, the below output is obtained -
Item: Mobile, Price: 500
Using findall() function
The re.findall() function returns a list containing all matches, that is list of strings with non-digits.
Example
In the following example, let us assume 'Today's temperature is 40.5 degrees.' as a string. Here, we need to extract the decimal number 40.5 from the string.
import re string = "Today's temperature is 40.5 degrees." x=re.findall(r"\d+\.\d+",string) print(x)
On executing the above code snippet, the below output is obtained.
['40.5']