Found 26504 Articles for Server Side Programming

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

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

317 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

942 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

896 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

Explain Python regular expression search vs match

SaiKrishna Tavva
Updated on 17-Apr-2025 18:50:53

366 Views

The built-in Python module re provides re.search() and re.match(), which are powerful regular expression functions. They are commonly used functions for finding patterns in strings, but they behave differently. The re.search() Function The re.search() function checks the entire string for a match. It will return the first match it finds in the string, not just at the beginning. This is helpful when the pattern might appear in the middle or end of the string. If it finds a match, it returns a Match object; if not, it returns None. Example In the following example re.search() function looks through the whole ... Read More

How do we use Python Regular Expression named groups?

SaiKrishna Tavva
Updated on 15-Apr-2025 12:25:01

1K+ Views

Usually, if we need to find a particular word such as, "data" in a text or a string we will directly search for it. We can also use a sequence of characters which forms a pattern to search for words in a string. For example the characters "[0-9]" matches a single digit number and "[0-9]+" matches any string containing one or more digits. These sequence of characters are known as regular expressions. Groups in Python Regular Expressions Like any other programming Python provides a module named re to handle regular expressions. The methods re.match() and re.search() of this module are ... Read More

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

SaiKrishna Tavva
Updated on 15-Apr-2025 13:00:47

235 Views

Python's, regular expressions (regex) allow us to search, extract, and manipulate patterns in strings. Sometimes, If we want to extract only numbers from a string, except decimals, then we can use re.findall() method with a specific regex pattern. The regex pattern was designed to specifically match the integer pattern. Let us first understand the regular expression used: \b\d+\b 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). ... Read More

What does “?:” mean in a Python regular expression?

Rajendra Dharmkar
Updated on 13-Jun-2020 06:20:09

2K+ Views

Non capturing groupsIf  we do not want a group to capture its match, we can write this regular expression as Set(?:Value). The question mark and the colon after the opening parenthesis are the syntax that creates a non-capturing group. The regex Set(Value)? matches Set or SetValue. In the first case, the first (and only) capturing group remains empty. In the second case, the first capturing group matches Value. The question mark appearing at the end is the quantifier that makes the previous token optional. Set(?:Value) matches Setxxxxx, i.e., all those strings starting with Set but not followed by Value. Such would be ... Read More

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

Akshitha Mote
Updated on 15-Apr-2025 12:34:02

906 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. The following is a syntax ... Read More

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

Nikitasha Shrivastava
Updated on 06-Jun-2025 15:29:37

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 or this module are re.search(), re.findall() and re.match() to make it easier to extract desired data. Extract Data Using Regular Expressions For extracting data we will have to define a pattern and apply it to a string with the help of regex functions. ... Read More

How to write Python regular expression to check alphanumeric characters?

Nikitasha Shrivastava
Updated on 06-Jun-2025 15:05:57

496 Views

This article will explain you how to write Python regular expression to check alphanumeric characters. Alphanumeric characters in Python are defined as letters like (a-z, A-Z) and numbers (0-9). If you want to check that the string has only alphanumeric characters like letters and numbers you can use Python's re module which makes this task easy. Regex Pattern for Alphanumeric Check Here is the regex pattern for checking alphanumeric characters - ^[a-zA-Z0-9]+$ The pattern (a-z) represents lowercase letters, (A-Z) means uppercase letters and (0-9) represents numbers from 0 to 9. And "^" is start of the string, ... Read More

Advertisements