Rajendra Dharmkar has Published 452 Articles

What is difference between self and __init__ methods in python Class?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:30:30

7K+ Views

selfThe word 'self' is used to represent the instance of a class. By using the "self" keyword we access the attributes and methods of the class in python.__init__ method"__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called ... Read More

How to create instance Objects using __init__ in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:29:40

721 Views

The instantiation or calling-a-class-object operation creates an empty object. Many classes like to create objects with instances with a specific initial state. Therefore a class may define a special method named __init__(), as follows −def __init__(self) −    self.data = [ ]When a class defines an __init__() method, class instantiation ... Read More

How do you validate a URL with a regular expression in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 07:28:42

486 Views

There's no validate method as almost anything is a valid URL. There are some punctuation rules for splitting it up. Without any punctuation, you still have a valid URL.Depending on the situation, we use following methods.If you trust the data, and just want to verify if the protocol is HTTP, ... Read More

How to use special characters in Python Regular Expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 07:12:45

6K+ Views

From Python documentationNon-special characters match themselves. Special characters don't match themselves −\ Escape special char or start a sequence..Match any char except newline, see re.DOTALL^Match start of the string, see re.MULTILINE $  Match end of the string, see re.MULTILINE[ ]Enclose a set of matchable charsR|S Match either regex R or regex S.()Create capture ... Read More

What are some basic examples of Python Regular Expressions?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 06:54:06

185 Views

Here are two basic examples of Python regular expressionsThe re.match() method finds match if it occurs at start of the string. For example, calling match() on the string ‘TP Tutorials Point TP’ and looking for a pattern ‘TP’ will match. However, if we look for only Tutorials, the pattern will ... Read More

How do I clear the regular expression cache in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 06:52:00

529 Views

Presently, when regular expressions are compiled, the result is cached so that if the same regex is compiled again, it is retrieved from the cache and no extra effort is required. This cache supports up to 100 entries. Once the 100th entry is reached, the cache is cleared and a ... Read More

What is the difference between re.search() and re.findall() methods in Python regular expressions?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 06:35:06

1K+ Views

The re.search() method is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only. Exampleimport re result = re.search(r'Tutorials', 'TP Tutorials Point TP') print result.group()OutputTutorialsHere you can see that, search() method is able to find a pattern from any position of the string.The ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

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

1K+ 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 ... Read More

How do we use Python Regular Expression named groups?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 06:19:37

666 Views

Named GroupsMost modern regular expression engines support numbered capturing groups and numbered backreferences. Long regular expressions with lots of groups and backreferences can be difficult to read and understand. More over adding or removing a capturing group in the middle of the regex disturbs the numbers of all the groups ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

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

311 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. ^ $ * + ? { } ... Read More

Advertisements