Rajendra Dharmkar has Published 452 Articles

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

Rajendra Dharmkar

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 ... Read More

How to write Python Regular Expression find repeating digits in a number?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 05:55:55

540 Views

The following code using Python regex to find the repeating digits in given stringExampleimport re result = re.search(r'(\d)\1{3}','54222267890' ) print result.group()OutputThis gives the output2222

How do backslashes work in Python Regular Expressions?

Rajendra Dharmkar

Rajendra Dharmkar

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

493 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; ... Read More

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

Rajendra Dharmkar

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 ... Read More

How to write Python regular expression to check alphanumeric characters?

Rajendra Dharmkar

Rajendra Dharmkar

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

282 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

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

Rajendra Dharmkar

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.45Read More

How to write a Python Regular Expression to validate numbers?

Rajendra Dharmkar

Rajendra Dharmkar

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

223 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 match multiple words anywhere?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 05:31:13

3K+ Views

The following code using Python regex matches the given multiple words in the given stringExampleimport re s = "These are roses and lilies and orchids, but not marigolds or .." r = re.compile(r'\broses\b | \bmarigolds\b | \borchids\b', flags=re.I | re.X) print r.findall(s)OutputThis gives the output['roses', 'orchids', 'marigolds']Read More

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

Rajendra Dharmkar

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 01Read More

How to write Python regular expression to match with file extension?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 05:06:54

626 Views

The following code using regex matches the file extension in the given file name.Exampleimport re result = re.search('.doc$', '87654_3.doc') print result.group()OutputThis gives the output.doc

Advertisements