- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract numbers from text using Python regular expression?
If we want to extract all numbers/digits individually from given text we use the following regex
Example
import re s = '12345 abcdf 67' result=re.findall(r'\d', s) print result
Output
['1', '2', '3', '4', '5', '6', '7']
If we want to extract groups of numbers/digits from given text we use the following regex
Example
import re s = '12345 abcdf 67' result=re.findall(r'\d+', s) print result
Output
['12345', '67']
Advertisements