Found 10784 Articles for Python

How to return the current CPU time in Python?

Pranav Indukuri
Updated on 02-Nov-2023 13:19:28

4K+ Views

In this article, we retrieve the current CPU time in Python. We use 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.time() method to get the current CPU time in seconds. The time is calculated since the epoch. It returns a floating-point number expressed in seconds. 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 time in ... Read More

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

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

896 Views

We come across negated character classes in Python regular expressions. An regex of ‘[abdfgh]’ matches any single character which is one of ‘a’, ‘b’, ‘d’, ’f’, ’g’ or ‘h’. This is termed a character class.An regex of ‘[^abdfgh]’ will match any single character which is NOT one of ‘a’, ‘b’, ‘d’, ’f’, ’g’ or ‘h’. This is a negated character class, and is indicated by the ‘^’ character at the start of the character class.The character ‘^’ has a special meaning at the start of the character class. if it is used elsewhere in that character class it simply means a ... Read More

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

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

314 Views

Most letters and characters simply match themselves. However, there are some characters called metacharacters, that don’t match themselves. Instead, they indicate that some pattern should be matched, or they repeat or change parts of the regular expression. Here’s a complete list of the metacharacters. ^ $ * + ? { } [ ] \ | ( )At first we’ll look at [ and ]. They’re used for indicating a character class, which is a set of characters that you want to match. Characters can be listed individually, or a range of characters can be indicated by giving two characters and separating ... Read More

What are repeating character classes used in Python regular expression?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

436 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 19-May-2023 13:01:03

1K+ Views

Character class operations are a way to match certain types of characters in a string using regular expressions in Python. In regular expressions, a "character class" is a set of characters that you want to match. You can use square brackets `[]` to create a character class. For example, if you want to match any vowel, you can use the character class `[aeiou]`. This will match any single character that is either 'a', 'e', 'i', 'o', or 'u'. Example This code will search for all the vowels in the `text` string using the regular expression `[aeiou]`, and it will return ... Read More

How does nested character class subtraction work in Python?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

308 Views

Nested Character Class SubtractionSince we can use the full character class syntax within the subtracted character class, we can subtract a class from the class being subtracted. [0-9-[0-7-[0-3]]] first subtracts 0-3 from 0-7, yielding [0-9-[4-7]], or [0-38-9], which matches any character in the string 012389.The class subtraction is always the last element in the character class. [0-9-[4-7]a-d] is not a valid regular expression. It should be rewritten as [0-9a-d-[4-7]]. The subtraction works on the whole class. While we can use nested character class subtraction, we cannot subtract two classes sequentially. To subtract ASCII characters and Arabic characters from a class with ... Read More

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

Rajendra Dharmkar
Updated on 02-Nov-2023 13:22:22

4K+ Views

The following code uses a regular expression '(a|b)' to match a or b in the given Python string.We are also using the flag re.I to ignore case of a or b while matching. Example import re s = 'Bank of Baroda' print(re.findall(r'(a|b)',s, re.I))OutputThis gives the output['B', 'a', 'B', 'a', 'a']

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

Rajendra Dharmkar
Updated on 19-Feb-2020 10:23:56

830 Views

The following code matches anything except space and new line in the given string using regex.Exampleimport re print re.match(r'^[^ ]*$', """IfindTutorialspointuseful""") print re.match(r'^[^ ]*$', """I find Tutorialspointuseful""") print re.match(r'^[^ ]*$', """Ifind Tutorialspointuseful""")OutputThis gives the output None None

How to remove tabs and newlines using Python regular expression?

Rajendra Dharmkar
Updated on 19-Dec-2019 09:02:07

673 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

1K+ 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