Found 27104 Articles for Server Side Programming

How do i match just spaces and newlines with Python regex?

Rajendra Dharmkar
Updated on 19-Feb-2020 12:42:18

454 Views

The following matches and prints just spaces and newlines in the given string using Python regexExampleimport re foo = ' I find   Tutorialspoint   useful' result = re.findall(r'\s+', foo) print resultOutputThis gives the output[' ', ' ', ' ', ' ']

Explain Python regular expression search vs match

Rajendra Dharmkar
Updated on 19-Feb-2020 12:44:16

216 Views

Both re.match() and re.search() are methods of the Python module re.The re.match() method finds match if it occurs at start of the string. For example, calling match() on the string ‘TP Tutorials Point TP’ and looking for a pattern ‘TP’ will match.Exampleresult = re.match(r'TP', 'TP Tutorials Point TP') print result.group(0)OutputTPThe re.search() method is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only. Exampleresult = re.search(r'Tutorials', 'TP Tutorials Point TP') print result.group(0)OutputTutorialsHere you can see that, search() method is able to find a pattern from any position of the string.Read More

How do we use Python Regular Expression named groups?

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

656 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

165 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

620 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

778 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

278 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

Advertisements