Rajendra Dharmkar has Published 452 Articles

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

Rajendra Dharmkar

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')

Explain Python regular expression search vs match

Rajendra Dharmkar

Rajendra Dharmkar

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

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

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

Rajendra Dharmkar

Rajendra Dharmkar

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

462 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[' ', ' ', ' ', ' ']

What is recursion & backtracking in Python?

Rajendra Dharmkar

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

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

Rajendra Dharmkar

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']

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

Rajendra Dharmkar

Rajendra Dharmkar

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

424 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', ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

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

754 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 find and replace within a text file using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 12:13:43

336 Views

The following code does the replacement in the given text file. After the replacement, the text is written to a new text file 'bar.txt'Examplef1 = open('foo.txt', 'r') f2 = open('bar.txt', 'w') for line in f1:     print line     f2.write(line.replace('Poetry', 'Prose')) f2 = open('bar.txt', 'r') for line in f2: ... Read More

How to match anything except space and new line using Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 10:23:56

827 Views

The following code matches anything except space and new line in the given string using regex.Exampleimport re print re.match(r'^[^ ]*$', """IfindTutorialspointuseful""") print re.match(r'^[^ ]*$', """I find Tutorialspointuseful""") print re.match(r'^[^ ]*$', """Ifind Tutorialspointuseful""")OutputThis gives the output None None

What are Python modules for date manipulation?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 19-Feb-2020 09:46:01

217 Views

There are many modules available in both the standard library and the PiPy repository for date manipulation. The most popular among these libraries are the following(in no particular order) −datetime (Standard library) − The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While ... Read More

Advertisements