Found 27104 Articles for Server Side Programming

Why do we use re.compile() method in Python regular expression?

Rajendra Dharmkar
Updated on 02-Nov-2023 14:06:21

12K+ Views

The re.compile() methodre.compile(pattern, repl, string): We can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search a pattern again without rewriting it. Example import re pattern=re.compile('TP') result=pattern.findall('TP Tutorialspoint TP') print result result2=pattern.findall('TP is most popular tutorials site of India') print result2Output['TP', 'TP'] ['TP']

How to optimize the performance of Python regular expression?

Md Waqar Tabish
Updated on 02-Nov-2023 14:14:07

3K+ Views

Introduction A regular expressions -specific built-in library named re exists in Python. You only need to import it to use its features (such as search, match, findall, etc.). They'll provide you back a Match object with helpful techniques for modifying your outcomes. According to Wikipedia, regular expressions (also known as regexp) are collections of characters that specify a search pattern. It is a tool that enables you to filter, extract, or alter a series of characters. It has also been discovered that regular expressions function more quickly when the "in" operator is used. Regular expressions have performance difficulties and are ... Read More

How do we use Python regular expression to match a date string?

Md Waqar Tabish
Updated on 02-Nov-2023 14:18:49

5K+ Views

Introduction Programming languages frequently employ date inputs to obtain user data, such as birthdates, travel dates, reservation dates, etc. These dates given by the user may be immediately verified as legitimate using regular expressions. To determine whether a text has a valid date format and to extract a valid date from a string, utilize regular date expressions. When checking dates, a regular expression for dates (YYYY-MM-DD) should look for four digits at the beginning of the expression, a hyphen, a two-digit month between 01 and 12, another hyphen, and then a two-digit day between 01 and 31. This is how ... Read More

How do we use re.finditer() method in Python regular expression?

Rajendra Dharmkar
Updated on 02-Nov-2023 14:22:39

15K+ Views

According to Python docs, re.finditer(pattern, string, flags=0)Return an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result. The following code shows the use of re.finditer() method in Python regexExample import re s1 = 'Blue Berries' pattern = 'Blue Berries' for match in re.finditer(pattern, s1):     s = match.start()     e = match.end()     print 'String match "%s" at %d:%d' % (s1[s:e], s, e)OutputStrings match "Blue Berries" at 0:12

What is Raw String Notation in Python regular expression?

Md Waqar Tabish
Updated on 02-Nov-2023 20:45:56

5K+ Views

Introduction A regular expression is a word that is frequently abbreviated as regex. Regex is a set of characters that specifies a search pattern and is mostly used in text processors and search engines to execute find and replace operations. When a string in Python is prefixed with the letter r or R, as in r'...' and R'...', it becomes a raw string. In contrast to a conventional string, a raw string considers backslashes () as literal characters. When working with strings that include a lot of backslashes, such as regular expressions or directory paths on Windows, raw strings are ... Read More

How to get file creation & modification date/times in Python?

Pranav Indukuri
Updated on 02-Nov-2023 21:07:23

11K+ Views

They are various ways to get the file creation and modification datetime in Python. We will use different methods from the OS and pathlib module to get the file creation and modification datetime in python. Using OS Module: File Creation Time On Windows Here we have used the OS module to find the creation time of a file. Initially, we need to import OS module and datetime module. The OS module is used for getting the timestamp whereas the datetime module is used for creating a datetime object. os.path.getctime('path') function is used to get the creation time of a file. ... Read More

How to convert pandas offset to Python date?

Pranav Indukuri
Updated on 07-Sep-2022 07:02:26

480 Views

In this article, we convert the pandas offset to date in python. When you subtract the pandas from a date object, you get a pandas Timestamp object. You can convert this object to a String format date or Date object(Standard Python date). Or you can use the timedelta object from the datetime library. Example 1 In the following example code, we remove the offset of 10 days using to_offset(“10D”) from the pandas pd.to_datetime(‘2018-01-04’). from pandas.tseries.frequencies import to_offset import pandas as pd dt = pd.to_datetime('2018-01-04') - to_offset("10D") print(type(dt)) print(dt) Output The output of the above code is as follows. ... Read More

How do we compare Python Dates?

Pranav Indukuri
Updated on 05-Sep-2022 10:22:28

716 Views

In this article, we will understand how to compare python dates. They are different methods to identify which date is greater or lesser and these methods will be explored in detail. Using the timedelta() method and operator In this method, we use the datetime module and operator to compare two dates. To alter date and time, the datatime module provides timedelta() method. The timedelta() method takes the number of days as an input and returns the date. This method is used to perform arithmetic manipulations. Syntax The syntax of the timedelta() method from the datetime module in python ... Read More

How to convert a Python date string to a date object?

Pranav Indukuri
Updated on 02-Nov-2023 21:12:16

8K+ Views

In this article we convert a date string to a date object. We use the strptime() method to convert a date string to a date object. Strptime() The strptime() method takes the date as an input and converts it into a date object. Note that you cannot convert any string into a datetime object. The string should be in a certain format. Syntax The syntax of the strptime() method is shown in this section. datetime.strptime(time_data, format_data) Where, time_date – it is the time present in string format. format_date – it is the data(string) present in datetime format which is ... Read More

How can I subtract a day from a Python date?

Md Waqar Tabish
Updated on 27-Aug-2023 12:44:08

24K+ Views

Introduction It is essential to have a module that can modify date and time since, as we all know, they are utilized in applications where we must keep track of date and time. A DateTime module in Python deals with dates and times (Python Date/Time Tutorial). Python comes with a built-in datetime module. Installation of two new libraries is necessary before any data modification may take place. Dates and timings are quickly retrieved using the arrow library. A DataFrame may be accessed and used thanks to the Pandas library. Go to an IDE console to install these libraries. ... Read More

Advertisements