Found 10784 Articles for Python

How to match whitespace but not newlines using Python regular expressions?

Rajendra Dharmkar
Updated on 19-Feb-2020 12:35:38

757 Views

The following code matches only the whitespaces but not newlines in the given stringExampleimport re print re.findall(r"(\s){1,}","""I find     Tutorialspoint useful""")OutputThis gives the output[' ', ' ', ' ']

How to strip spaces/tabs/newlines using Python regular expression?

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

429 Views

The following code strips the spaces/tabs/newlines from given string using Python regexWe use '\S' in regex which stands for all non-whitespace charactersExampleimport re print re.findall(r"[\S]","""I find     Tutorialspoint useful""")OutputThis gives the output['I', 'f', 'i', 'n', 'd', 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't', 'u', 's', 'e', 'f', 'u', 'l']

How to split on successions of newline characters using Python regular expression?

Rajendra Dharmkar
Updated on 19-Feb-2020 12:38:33

144 Views

The following code splits on successions of newline characters in the following string using Python regexExampleimport re s = """I find  Tutorialspoint   useful""" print re.split(r"[]", s)OutputThis gives the output['I find', '    Tutorialspoint', '     useful']

What is recursion & backtracking in Python?

Rajendra Dharmkar
Updated on 19-Feb-2020 12:40:15

677 Views

Recursion Recursion is useful in dividing and solving problems. Each recursive call itself spins off other recursive calls. At the centre of a recursive function are two types of cases: base cases, which tell the recursion when to terminate, and recursive cases that call the function they are in. A simple problem that naturally uses recursive solutions is calculating factorials. The recursive factorial algorithm has two cases: the base case when n = 0, and the recursive case when n>0 . BacktrackingBacktracking is a general algorithm for finding solutions to some computational problem, that incrementally builds choices to the solutions, and rejects ... Read More

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

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

464 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

220 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

668 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

166 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

628 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