Rajendra Dharmkar

Rajendra Dharmkar

160 Articles Published

Articles by Rajendra Dharmkar

Page 7 of 16

How to subtract Python timedelta from date in Python?

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

The Python datetime module provides various ways for manipulating dates and times. One of its key features is the timedelta object, which represents duration and also the difference between two dates or times. Subtracting a specific amount of time from a date can be done using timedelta. For example, if we want to find the date a day before today, then we create a timedelta object with days=1 and then subtract it from the current date ? Subtracting One Day from Today's Date The basic use case is subtracting a single day from today's date. This can ...

Read More

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

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 596 Views

Python's regular expressions provide various ways to search and manipulate strings. Metacharacters are special characters that carry specific meaning in regex patterns. However, their behavior changes significantly when used inside character classes (denoted by square brackets []). Understanding how metacharacters behave within character classes is crucial for writing accurate regular expressions. Most metacharacters lose their special meaning inside character classes, but some retain or alter their behavior. Understanding Character Classes Character classes are denoted by square brackets [] and define a set of characters that can match at a single position. For example, [aeiou] matches any single ...

Read More

What are repeating character classes used in Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 602 Views

A repeating character class in Python regular expressions is a character class followed by quantifiers like ?, *, or +. These quantifiers control how many times the entire character class should match. Basic Repeating Character Classes When you use quantifiers with character classes, they repeat the entire class, not just the specific character that was matched ? import re # [0-9]+ matches one or more digits (any combination) text = "Order 579 and 333 items" pattern = r'[0-9]+' matches = re.findall(pattern, text) print("Matches:", matches) Matches: ['579', '333'] Common Quantifiers ...

Read More

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

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

What is the difference between re.findall() and re.finditer() methods available in Python?

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

This article guides you through the difference between re.findall() and re.finditer() methods available in Python. The re module of Python helps in working with regular expressions. Both the re.findall() and re.finditer() methods are used to search for patterns, but they behave differently. Let us see the differences with simple explanations and examples in this article. Using re.findall() Method The re.findall() method returns a list of all matching patterns found in the string. It searches through the entire string and collects all non-overlapping matches. Example Here is an example to show the usage of the findall() ...

Read More

How does repetition operator work on list in Python?

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

In Python, the * symbol serves as the repetition operator when used with lists. Instead of multiplication, it creates multiple copies of a list and concatenates them together into a single new list. Basic Syntax The repetition operator follows this pattern ? # Syntax: list * number new_list = original_list * repetition_count Single Element Repetition Create a list with repeated single elements ? numbers = [0] * 5 print(numbers) [0, 0, 0, 0, 0] The list [0] contains one element. The repetition operator makes 5 copies ...

Read More

How to divide a string by line break or period with Python regular expressions?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 432 Views

When working with text processing in Python, you often need to split strings by multiple delimiters like periods and line breaks. Python's regular expressions module re provides powerful pattern matching for this purpose. Using re.findall() to Split by Period and Line Break The re.findall() function extracts all substrings that match a given pattern. Here's how to split a string by periods and line breaks − import re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print(result) ['Hi', "It's nice meeting you", 'My name is ...

Read More
Showing 61–70 of 160 articles
« Prev 1 5 6 7 8 9 16 Next »
Advertisements