Articles on Trending Technologies

Technical articles with clear explanations and examples

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

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 6K+ Views

In Python regular expressions, one of the common tasks is matching either one character or another. This can be done 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 with different methods. Using re.search() with the | Symbol The re.search() method 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. ...

Read More

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

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 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 Regular Expression Patterns ...

Read More

How to remove tabs and newlines using Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 1K+ Views

Regular expressions provide a powerful way to remove tabs and newlines from strings. Python's re.sub() function can replace whitespace characters including tabs (\t) and newlines () with spaces or remove them entirely. Basic Approach Using \s+ The \s+ pattern matches one or more whitespace characters (spaces, tabs, newlines) and replaces them with a single space ? import re text = """I find Tutorialspoint helpful""" result = re.sub(r"\s+", " ", text) print(result) I find Tutorialspoint helpful Removing Only Tabs and Newlines To target only tabs and newlines while preserving ...

Read More

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

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 2K+ Views

In Python, regular expressions (regex) provide powerful tools to search and manipulate strings. When you need to match whitespace characters like spaces and tabs but exclude newline characters, you can use specific regex patterns with Python's re module. The following methods demonstrate how to match whitespace but not newlines using Python regular expressions ? Using re.sub() Method Using re.findall() Method Using Character Classes Using Positive Lookahead Using re.sub() Method The re.sub() method efficiently replaces whitespace characters (excluding newlines) ...

Read More

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

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 1K+ Views

Python regular expressions (regex) provide powerful methods to strip whitespace characters including spaces, tabs, and newlines from strings. The re module offers several approaches to handle different whitespace scenarios. This article explains how to strip various types of whitespace using regular expressions, covering the following methods ? Stripping All Whitespace Using re.sub() Stripping Leading/Trailing Whitespace Splitting on Whitespace Characters Stripping Specific Whitespace Types Stripping All Whitespace Using re.sub() The re.sub() function replaces all occurrences of a pattern with ...

Read More

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

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 505 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. Example The following code splits the text ...

Read More

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

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 408 Views

Python's regular expressions (regex) allow us to search, extract, and manipulate patterns in strings. When we want to extract only whole numbers from a string (excluding decimals), we can use re.findall() method with a specific regex pattern. The regex pattern \b\d+\b is designed to match integer patterns specifically. This pattern matches numbers like 10, 245, 89 but not 12.34 or 0.56 (decimals). \b − Word boundary to ensure we match standalone numbers \d+ − One or more digits (0−9) \b − Ending word boundary Basic Extraction ...

Read More

What does "?:" mean in a Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

In Python regular expressions, the ?: syntax creates a non-capturing group. This allows you to group parts of a pattern without storing the matched text for later retrieval. What are Non-Capturing Groups? A non-capturing group groups regex tokens together but doesn't create a capture group that you can reference later. The syntax is (?:pattern) where ?: immediately follows the opening parenthesis. Example import re # Non-capturing group pattern = r'Set(?:Value)' text = "SetValue" match = re.search(pattern, text) if match: print(f"Full match: {match.group(0)}") print(f"Number of groups: ...

Read More

How to write Python regular expression to match with file extension?

Akshitha Mote
Akshitha Mote
Updated on 24-Mar-2026 1K+ Views

Using Python, we can easily search for specific types of files from a mixed list of file names using regular expressions. For this, we need to import Python's built-in module called re using the import keyword. The Regular expression or Regex is a special sequence of characters like \, *, ^, etc, which are used to search for a pattern in a string or a set of strings. It can detect the presence or absence of characters by matching them with a particular pattern and can also split a string into one or more substrings. Importing the Regex ...

Read More

How to extract data from a string with Python Regular Expressions?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 1K+ Views

In this article you will find out how to extract data from a string with Python Regular Expressions. In Python, extracting data from the given string is a common task. Regular expressions (regex) offer pattern-matching functionality to get and identify specific parts of a string. Python's re module helps in working with regex easily. The common functions of this module are re.search(), re.findall() and re.match() to make it easier to extract desired data. Common Regular Expression Functions Function Purpose Returns re.findall() Find all matches List of strings re.search() Find first match ...

Read More
Showing 7571–7580 of 61,297 articles
« Prev 1 756 757 758 759 760 6130 Next »
Advertisements