Found 34477 Articles for Programming

How do we use Python Regular Expression named groups?

Rajendra Dharmkar
Updated on 13-Jun-2020 06:19:37

671 Views

Named GroupsMost modern regular expression engines support numbered capturing groups and numbered backreferences. Long regular expressions with lots of groups and backreferences can be difficult to read and understand. More over adding or removing a capturing group in the middle of the regex disturbs the numbers of all the groups that follow the added or removed group.Python's re module was the first to come up with a solution: named capturing groups and named backreferences. (?Pgroup) captures the match of group into the backreference "name". name must be an alphanumeric sequence starting with a letter. group can be any regular expression. ... Read More

How to write a Python regular expression to get numbers except decimal?

Rajendra Dharmkar
Updated on 19-Feb-2020 12:50:29

167 Views

The following code gets the numbers in the given string except the decimal>>> m = re.match(r"(\d+)\.(\d+)", "80.3196") >>> m.groups() ('80', '3196')

What does “?:” mean in a Python regular expression?

Rajendra Dharmkar
Updated on 13-Jun-2020 06:20:09

1K+ Views

Non capturing groupsIf  we do not want a group to capture its match, we can write this regular expression as Set(?:Value). The question mark and the colon after the opening parenthesis are the syntax that creates a non-capturing group. The regex Set(Value)? matches Set or SetValue. In the first case, the first (and only) capturing group remains empty. In the second case, the first capturing group matches Value. The question mark appearing at the end is the quantifier that makes the previous token optional. Set(?:Value) matches Setxxxxx, i.e., all those strings starting with Set but not followed by Value. Such would be ... Read More

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

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

632 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

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

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

795 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

287 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

809 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

225 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'(?

Advertisements