Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How does nested character class subtraction work in Python?
Nested character class subtraction in Python's regular expressions allows us to define complex character sets by removing specific characters from an existing character class using the '-' operator within square brackets. Python's built-in re module doesn't support nested character class subtraction directly. Instead, we need to use the third-party regex module, which can be installed using pip install regex. Syntax The basic syntax for character class subtraction is ? [character_set--[characters_to_exclude]] For example, [0-9--[4-6]] matches digits 0-9 but excludes 4, 5, and 6, effectively matching 0, 1, 2, 3, 7, 8, and 9. ...
Read MoreHow to write a regular expression to match either a or b in Python?
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 MoreHow to match anything except space and new line using Python regular expression?
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 MoreHow to remove tabs and newlines using Python regular expression?
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 MoreHow to match whitespace but not newlines using Python regular expressions?
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 MoreHow to strip spaces/tabs/newlines using Python regular expression?
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 MoreHow to split on successions of newline characters using Python regular expression?
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 MoreHow to write a Python regular expression to get numbers except decimal?
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 MoreWhat does "?:" mean in a Python regular expression?
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 MoreHow to write Python regular expression to match with file extension?
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