Found 10784 Articles for Python

How do we use a delimiter to split string in Python regular expression?

Rajendra Dharmkar
Updated on 20-Feb-2020 07:13:40

237 Views

The re.split() methodre.split(pattern, string, [maxsplit=0]):This methods helps to split string by the occurrences of given pattern.Exampleimport re result=re.split(r'a', 'Dynamics') print resultOutput['Dyn', 'mics']Above, we have split the string “Dynamics” by “a”. Method split() has another argument “maxsplit“. It has default value of zero. In this case it does the maximum splits that can be done, but if we give value to maxsplit, it will split the string. ExampleLet’s look at the example below −import result=re.split(r'a', 'Dynamics Kinematics') print resultOutput['Dyn', 'mics Kinem', 'tics']ExampleConsider the following codeimport re result=re.split(r'i', 'Dynamics Kinematics', maxsplit=1) print resultOutput['Dyn', 'mics Kinematics']Here, you can notice that we have fixed the ... Read More

How to escape all special characters for regex in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 07:09:40

1K+ Views

We use re.escape() to escape the special characters −The following code shows how all special characters in given string are escaped using re.escape() method>>> p = '5*(67).89?' >>> re.escape(p) '5\*\(67\)\.89\?'

How to escape any special character in Python regular expression?

Md Waqar Tabish
Updated on 04-Apr-2023 12:20:10

1K+ Views

Regex, often known as regexp, is a potent tool for finding and manipulating text strings, especially when processing text files. Regex may easily replace many hundred lines of computer code with only one line. All scripting languages, including Perl, Python, PHP, JavaScript, general-purpose programming languages like Java, and even word processors like Word, support Regex for text searching. Regex may be challenging to learn because of its complicated syntax, but it is time well spent. Special Characters Text processing becomes more challenging when special characters are included because context must be carefully considered. You must think about what you see, ... Read More

Why do we use question mark literal in Python regular expression?

Md Waqar Tabish
Updated on 02-Nov-2023 14:01:58

2K+ Views

Introduction The question mark makes the previous token in the regular expression optional. For example: colou?r is complementary to both colour and colour. A quantifier is what the question mark is known as. You may make multiple tokens optional by combining numerous tokens in parentheses and adding the question mark after the final set of parentheses. Like Nov(ember)? matches between Nov and Nov. Using many question marks, you may create a regular expression matching a wide range of options. Feb(ruary)? 23(rd)? Matches February 23rd, February 23, Feb 23rd and Feb 23. Curly braces can also be used to make something ... Read More

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

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

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

Advertisements