
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to extract data from a string with Python Regular Expressions?
The following code extracts data like first_id, second_id, category from given strings
Example
import re s = 'TS001B01.JPG' match = re.match(r'(TS\d+)([A|B])(\d+)\.JPG', s) first_id = match.group(1) category = match.group(2) second_id = match.group(3) print first_id print category print second_id
Output
This gives output
TS001 B 01
- Related Articles
- How to extract numbers from a string using regular expressions?
- How to divide a string by line break or period with Python regular expressions?
- How to remove consonants from a string using regular expressions in Java?
- How to remove vowels from a string using regular expressions in Java?
- How to extract numbers from text using Python regular expression?
- How to extract date from text using Python regular expression?
- How to extract numbers from a string in Python?
- How to extract date from a string in Python?
- How to extract numbers from a string using Python?
- Replace one string with another string with Java Regular Expressions
- How to extract each (English) word from a string using regular expression in Java?
- How to extract floating number from text using Python regular expression?
- How to extract email id from text using Python regular expression?
- Python – How to Extract all the digits from a String
- How to extract number from string in R data frame?

Advertisements