Programming Articles - Page 3266 of 3366

How to return the current CPU time in Python?

Pranav Indukuri
Updated on 28-Aug-2025 12:14:05

5K+ Views

In this article, we retrieve the current CPU time in Python using the time() method,  which is imported from the Python time module. The time module in Python provides various methods and functions related to time. Here we use the time.The time() method to get the current CPU time in seconds. The time is calculated since the epoch. This method returns a float value that represents the seconds since the epoch. Epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the ... Read More

What are negated character classes that are used in Python regular expressions?

SaiKrishna Tavva
Updated on 15-May-2025 19:40:32

1K+ Views

 While working with Python regex,  if we want to match everything except certain characters, then we can use negated character classes by placing a caret (^) as the first character inside square brackets. The pattern [^abdfgh] will match any character not in that set. What is a Negated Character Class? A character class like [abc] matches any single character except 'a', 'b', or 'c'. But if we use a ^ symbol at the beginning, like [abc], it will match any character except 'a', 'b', or 'c'. This allows us to eliminate certain characters from the match quickly. The position of ... Read More

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

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

485 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

562 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

416 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

974 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['']

Advertisements