Found 10476 Articles for Python

How does nested character class subtraction work in Python?

SaiKrishna Tavva
Updated on 15-May-2025 19:42:08

409 Views

Nested character class subtraction in Python's regular expressions allows us to define complex character sets by removing characters from an existing character class, including another character class by using the '-' operator. It works from the innermost to outermost character class, and subtractions are evaluated from left to right within the square brackets [ ]. Usually, Python's library, re, doesn't directly support nested character class subtraction like some other regex engines do. We can't write something like [[abc]-[bc]], which means characters a, b, or c, excluding b or c. So we need to install a third-party module like regex ... Read More

How to write a regular expression to match either a or b in Python?

Rajendra Dharmkar
Updated on 28-Aug-2025 12:12:51

5K+ Views

In Python regular expressions, one of the common tasks is matching either one character or another. we can done this easily by using the re module along with the pipe symbol |, which acts as an OR operator. In this article, we’ll explore how to match either 'a' or 'b' in a string using regex. The following are the various methods included. Using re.search() with the | symbol The re.search() combined with the | (OR) symbol allows us to search for multiple patterns within a single string. It returns a match object if any of the patterns are found. The ... Read More

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

Rajendra Dharmkar
Updated on 28-Aug-2025 11:48:56

1K+ Views

Python's re module provides various tools for pattern matching using regular expressions (regex). With the help of regex, we can define flexible patterns that match or exclude particular characters or sequences. In this article, we will focus on how to match everything except spaces and newlines using regular expressions. The following are the methods involved to match the regex pattern, except for space and new line. Using re.findall() Method Using re.split() Method Replace Spaces and Newlines with Empty String Match Words Without Space/Newline Using re.findall() Instead ... Read More

How to remove tabs and newlines using Python regular expression?

Rajendra Dharmkar
Updated on 19-Dec-2019 09:02:07

952 Views

The following code removes tabs and newlines from given stringExampleimport re print re.sub(r"\s+", " ", """I find Tutorialspoint helpful""")OutputThis gives outputI find Tutorialspoint helpful

How to match tab and newline but not space using Python regular expression?

Rajendra Dharmkar
Updated on 19-Dec-2019 09:03:45

2K+ Views

The following code matches tab and newline but not space from given string using regex.Exampleimport re print re.findall(r"[\t]","""I find     Tutorialspoint useful""")OutputThis gives the output['']

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

SaiKrishna Tavva
Updated on 23-Apr-2025 19:39:11

1K+ Views

In Python, regular expressions (regex) search and manipulate strings in various ways. If we need to match whitespace characters without including newline characters. This article will explore how to achieve this using Python’s re module. The following are the methods included to match whitespace but not newlines using Python regular expressions - Using re.sub() Method Using re.findall() Method Using Positive Lookahead Using re.sub() Method The re.sub() method offers an efficient way to replace whitespace characters (excluding newlines) within a string. This method takes the pattern ... Read More

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

SaiKrishna Tavva
Updated on 21-Apr-2025 15:45:08

734 Views

Python regular expressions (regex) provide various ways to handle whitespaces, including spaces, tabs, and newline characters, which can be effectively stripped from strings using regex. This article will explain how to split a string on newline characters using different regular expressions, following are the various methods to achieve the present task. Using re.split(r"[]", text) Splitting on One or More Newlines Using Quantifier [+] Splitting on Newlines with Whitespace Using re.split(r"\s*", text) Using re.split(r"[]", text) The re.split() function splits a string wherever the specified regular expression pattern ... Read More

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

SaiKrishna Tavva
Updated on 21-Apr-2025 11:04:58

312 Views

Python's built-in splitlines() method and the split() method with as a delimiter are sufficient to split strings based on newline characters. This article will explore different approaches to splitting strings on sequences of newline characters using Python's regular expressions. Splitting on One or More Newlines The Python re.split() function uses a regular expression to split a string. We'll use the pattern +, which means one or more newlines. The re.split() will find where these newlines are, split the string there, and return a list of the resulting pieces. The re.split() function then splits the string at each ... Read More

What is recursion & backtracking in Python?

SaiKrishna Tavva
Updated on 22-Apr-2025 19:10:15

940 Views

In Python, recursion is a programming technique where a function calls itself to solve a problem. Backtracking means trying different options one by one and going back if one option doesn’t work. It helps in solving problems step by step, like puzzles or finding the right path. How Recursion and Backtracking Work Together Imagine navigating a maze. We are trying a path, and if it hits a dead-end, we go back and try another. That’s backtracking. The steps of trying and going back are done using recursive function calls. Backtracking frequently uses recursion to explore different possibilities ... Read More

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

SaiKrishna Tavva
Updated on 17-Apr-2025 18:46:17

892 Views

Python's built-in module re (regular expression) provides special characters to match spaces, newlines, tabs etc. spaces can be extracted using the pattern " " and newlines can be extracted using the pattern "" The following is a simple overview of these special characters- Whitespace Character \s : Matches any whitespace character. Tab \t : Matches a tab character. Newline : Matches a newline character. Vertical Tab \v : Matches a vertical tab character. Form Feed \f : Matches ... Read More

Advertisements