Found 10784 Articles for Python

How to extract data from a string with Python Regular Expressions?

Rajendra Dharmkar
Updated on 20-Feb-2020 05:18:00

787 Views

The following code extracts data like first_id, second_id, category from given stringsExampleimport 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_idOutputThis gives outputTS001 B 01

How to use variables in Python regular expression?

Rajendra Dharmkar
Updated on 02-Nov-2023 06:51:00

8K+ Views

The following code demonstrates the use of variables in Python regex.The variable cannot contain any special or meta characters or regular expression. We just use string concatenation to create a string.Example import re s = 'I love books' var_name = 'love' result = re.search('(.+)'+var_name+'(.+)',s) print result var_name = 'hate' s2 = 'I hate books' result = re.search('(.+)'+var_name+'(.+)',s2) print resultOutputThis gives the output

How to write Python regular expression to check alphanumeric characters?

Rajendra Dharmkar
Updated on 20-Feb-2020 05:51:30

284 Views

In Python there is a special sequence \w for matching alphanumeric and underscore when the LOCALE and UNICODE flags are not specified. Exampleimport re result = re.search(r'^\w+$', 'Tutorials123') print result.group()OutputTutorials123

What is the difference between re.search() and re.findall() methods in Python regular expressions?

Rajendra Dharmkar
Updated on 13-Jun-2020 06:35:06

1K+ Views

The re.search() method is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only. Exampleimport re result = re.search(r'Tutorials', 'TP Tutorials Point TP') print result.group()OutputTutorialsHere you can see that, search() method is able to find a pattern from any position of the string.The re.findall() helps to get a list of all matching patterns. It searches from start or end of the given string. If we use method findall to search for a pattern in a given string it will return all occurrences of the pattern. While searching a pattern, it is recommended to ... Read More

How to write a Python regular expression that matches floating point numbers?

Rajendra Dharmkar
Updated on 20-Feb-2020 05:49:15

802 Views

The following code uses Python regex to match floating point numbersExampleimport re s = '234.6789' match = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?',s) print match.group() s2 = '0.45' match = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?',s2) print match.group()OutputThis gives the output234.6789 0.45

How to write a Python Regular Expression to validate numbers?

Rajendra Dharmkar
Updated on 20-Feb-2020 05:45:45

224 Views

The following code validates a number exactly equal to '2018'Exampleimport re s = '2018' match = re.match(r'\b2018\b',s) print match.group()OutputThis gives the output2018ExampleThe following code validates any five digit positive integerimport re s = '2346' match = re.match(r'(?

How to write a Python regular expression to use re.findall()?

Rajendra Dharmkar
Updated on 20-Feb-2020 05:53:27

351 Views

The re.findall() helps to get a list of all matching patterns. It searches from start or end of the given string. If we use method findall to search for a pattern in a given string it will return all occurrences of the pattern. While searching a pattern, it is recommended to use re.findall() always, it works like re.search() and re.match() both.Exampleimport re result = re.search(r'TP', 'TP Tutorials Point TP') print result.group()OutputTP

How do backslashes work in Python Regular Expressions?

Rajendra Dharmkar
Updated on 20-Feb-2020 05:54:55

495 Views

According to Python docs, perhaps the most important metacharacter in regular expressions is the backslash, \. As in Python string literals, the backslash can be followed by various characters to indicate various special sequences. It’s also used to escape all the metacharacters so you can still match them in patterns; for example, if you need to match a [ or \, you can precede them with a backslash to remove their special meaning: \[ or \.The following code highlights the function of backslash in Python regexExampleimport re result = re.search('\d', '\d') print result result = re.search(r'\d', '\d') print result.group()OutputThis gives ... Read More

What is the difference between re.findall() and re.finditer() methods available in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 06:48:41

766 Views

The re.findall() methodThe re.findall() helps to get a list of all matching patterns. It searches from start or end of the given string. If we use method findall to search for a pattern in a given string it will return all occurrences of the pattern. While searching a pattern, it is recommended to use re.findall() always, it works like re.search() and re.match() both.Exampleimport re result = re.search(r'TP', 'TP Tutorials Point TP') print result.group()OutputTPThe re.finditer() methodre.finditer(pattern,  string,  flags=0) Return an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches ... Read More

How to use Python Regular expression to extract URL from an HTML link?

Bhanu Priya
Updated on 04-Oct-2023 18:14:33

2K+ Views

URL is an acronym for Uniform Resource Locator; it is used to identify the location resource on internet. For example, the following URLs are used to identify the location of Google and Microsoft websites − https://www.google.com https://www.microsoft.com URL consists of domain name, path, port number etc. The URL can be parsed and processed by using Regular Expression. Therefore, if we want to use Regular Expression we have to use re library in Python. Example Following is the example demonstrating URL − URL: https://www.tutorialspoint.com/courses If we parse the above URL we can find the website name and protocol ... Read More

Advertisements