Found 26504 Articles for Server Side Programming

What are metacharacters inside character classes used in Python regular expression?

Rajendra Dharmkar
Updated on 28-Aug-2025 11:42:12

472 Views

Python's regular expressions provide various ways to search and manipulate strings. They are used to define search patterns that can be matched in text data. These patterns are defined using a set of characters known as metacharacters, which carry special meaning in regex. Their behaviour can change when used inside character classes (also known as character sets or square brackets '[]'). Understanding how metacharacters are interpreted within character classes is crucial for writing accurate and effective regular expressions. Understanding Character Classes In Python regex, character classes are denoted by square brackets '[ ]', which define a set of characters or ... Read More

What are repeating character classes used in Python regular expression?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

554 Views

A character class followed by operators like '?', '*' or '+' are called repeating character classes.If you repeat a character class by using the '?', '*' or '+' operators, you will repeat the entire character class, and not just the character that it matched. The regex '[0-9]+' can match '579' as well as '333'. If you want to repeat the matched character, rather than the class, you will need to use backreferences. '([0- 9])\1+' will match '333' but not “579”. When applied to the string “922226”, it will match '2222' in the middle of this string. If you do not ... Read More

What are character class operations in Python?

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

2K+ Views

Character class operations in Python's regular expressions allow us to define set of characters we want to match. Instead of searching for one specific character, we can search for any character within that set. A character class in regex is written using square brackets []. It defines a group of characters where any character from the group can match a part of the string. Commonly Used Character Classes in Python (re module) Regular expressions use both normal and special characters. Normal characters like 'A', 'a', or '0' match themselves. So, "last" (a sequence of characters) matches the string 'last'. Some characters, ... Read More

How does nested character class subtraction work in Python?

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

410 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

956 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

738 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

Advertisements