![]() |
|
[Tut] Python Regex And Operator [Tutorial + Video] - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Regex And Operator [Tutorial + Video] (/thread-93495.html) |
[Tut] Python Regex And Operator [Tutorial + Video] - xSicKxBot - 02-07-2020 Python Regex And Operator [Tutorial + Video] This tutorial is all about the AND operator of Python’s re library. You may ask: what? (And rightly so.) Sure, there’s the OR operator (example: Code: 'iPhone|iPad'There are different interpretations for the AND operator in a regular expression (regex):
I’ll discuss both in the following. (You can also watch the video as you read the tutorial.) Ordered Python Regex AND Operator Given a string. Say, your goal is to find all substrings that match string Code: 'iPhone'Code: 'iPad'The straightforward AND operation of both strings is the regular expression pattern Code: iPhoneiPadIn the following example, you want to match pattern ‘aaa’ and pattern ‘bbb’—in this order. Code: >>> import re
>>> text = 'aaabaaaabbb'
>>> A = 'aaa'
>>> B = 'bbb'
>>> re.findall(A+B, text)
['aaabbb']
>>>You use the re.findall() method. In case you don’t know it, here’s the definition from the Finxter blog article: The re.findall(pattern, string) method finds all occurrences of the pattern in the string and returns a list of all matching substrings. Please consult the blog article to learn everything you need to know about this fundamental Python method. The first argument is the pattern Code: A+BCode: 'aaabbb'The second argument is the text Code: 'aaabaaaabbb'The result shows that there’s a matching substring in the text: Code: 'aaabbb'Unordered Python Regex AND Operator But what if you want to search a given text for pattern Code: ACode: BNow this is a bit more complicated because any regular expression pattern is ordered from left to right. A simple solution is to use the lookahead assertion Code: (?.*A)Code: ACode: .*Let’s first have a look at the minimal solution to check for two patterns anywhere in the string (say, patterns Code: 'hi'Code: 'you'Code: >>> import re
>>> pattern = '(?=.*hi)(?=.*you)'
>>> re.findall(pattern, 'hi how are yo?')
[]
>>> re.findall(pattern, 'hi how are you?')
['']In the first example, both words do not appear. In the second example, they do. But how does the lookahead assertion work? You must know that any other regex pattern “consumes” the matched substring. The consumed substring cannot be matched by any other part of the regex. Think of the lookahead assertion as a non-consuming pattern match. The regex engine goes from the left to the right—searching for the pattern. At each point, it has one “current” position to check if this position is the first position of the remaining match. In other words, the regex engine tries to “consume” the next character as a (partial) match of the pattern. The advantage of the lookahead expression is that it doesn’t consume anything. It just “looks ahead” starting from the current position whether what follows would theoretically match the lookahead pattern. If it doesn’t, the regex engine cannot move on. A simple example of lookahead. The regular expression engine matches (“consumes”) the string partially. Then it checks whether the remaining pattern could be matched without actually matching it.Let’s go back to the expression Code: (?=.*hi)(?=.*you)Code: 'hi'Code: 'you'The reason is that the lookahead expressions don’t consume anything. You first search for an arbitrary number of characters Code: .*Code: hiCode: youNote that this method doesn’t care about the order of the two words: Code: >>> import re
>>> pattern = '(?=.*hi)(?=.*you)'
>>> re.findall(pattern, 'hi how are you?')
['']
>>> re.findall(pattern, 'you are how? hi!')
['']No matter which word Code: "hi"Code: "you"You may ask: why’s the output the empty string? The reason is that the regex engine hasn’t consumed any character. It just checked the lookaheads. So the easy fix is to consume all characters as follows: Code: >>> import re
>>> pattern = '(?=.*hi)(?=.*you).*'
>>> re.findall(pattern, 'you fly high')
['you fly high']Now, the whole string is a match because after checking the lookahead with Code: '(?=.*hi)(?=.*you)'Code: '.*'Python Regex Not How can you search a string for substrings that do NOT match a given pattern? In other words, what’s the “negative pattern” in Python regular expressions? The answer is two-fold:
Here’s an example for the negative character class: Code: >>> import re
>>> re.findall('[^a-m]', 'aaabbbaababmmmnoopmmaa')
['n', 'o', 'o', 'p']And here’s an example for the negative lookahead pattern to match all “words that are not followed by words”: Code: >>> re.findall('[a-z]+(?![a-z]+)', 'hello world')
['hello', 'world']The negative lookahead Code: (?![a-z]+)Code: [a-z]+[Collection] What Are The Different Python Re Quantifiers? The “and”, “or”, and “not” operators are not the only regular expression operators you need to understand. So what are other operators? Next, you’ll get a quick and dirty overview of the most important regex operations and how to use them in Python. Here are the most important regex quantifiers: Quantifier | Description | Example Code: .The wild-card (‘dot’) matches any character in a string except the newline character ‘n’. | Regex ‘…’ matches all words with three characters such as ‘abc’, ‘cat’, and ‘dog’. Code: *The zero-or-more asterisk matches an arbitrary number of occurrences (including zero occurrences) of the immediately preceding regex. | Regex ‘cat*’ matches the strings ‘ca’, ‘cat’, ‘catt’, ‘cattt’, and ‘catttttttt’. Code: ?The zero-or-one matches (as the name suggests) either zero or one occurrences of the immediately preceding regex. | Regex ‘cat?’ matches both strings ‘ca’ and ‘cat’ — but not ‘catt’, ‘cattt’, and ‘catttttttt’. Code: +The at-least-one matches one or more occurrences of the immediately preceding regex. | Regex ‘cat+’ does not match the string ‘ca’ but matches all strings with at least one trailing character ‘t’ such as ‘cat’, ‘catt’, and ‘cattt’. Code: ^The start-of-string matches the beginning of a string. | Regex ‘^p’ matches the strings ‘python’ and ‘programming’ but not ‘lisp’ and ‘spying’ where the character ‘p’ does not occur at the start of the string. Code: $The end-of-string matches the end of a string. | Regex ‘py$’ would match the strings ‘main.py’ and ‘pypy’ but not the strings ‘python’ and ‘pypi’. Code: A|BThe OR matches either the regex A or the regex B. Note that the intuition is quite different from the standard interpretation of the or operator that can also satisfy both conditions. | Regex ‘(hello)|(hi)’ matches strings ‘hello world’ and ‘hi python’. It wouldn’t make sense to try to match both of them at the same time. Code: ABThe AND matches first the regex A and second the regex B, in this sequence. | We’ve already seen it trivially in the regex ‘ca’ that matches first regex ‘c’ and second regex ‘a’. Note that I gave the above operators some more meaningful names (in bold) so that you can immediately grasp the purpose of each regex. For example, the ‘^’ operator is usually denoted as the ‘caret’ operator. Those names are not descriptive so I came up with more kindergarten-like words such as the “start-of-string” operator. We’ve already seen many examples but let’s dive into even more! Code: import re text = ''' Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. ''' print(re.findall('.a!', text)) '''
Finds all occurrences of an arbitrary character that is
followed by the character sequence 'a!'.
['Ha!'] ''' print(re.findall('is.*and', text)) '''
Finds all occurrences of the word 'is',
followed by an arbitrary number of characters
and the word 'and'.
['is settled, and'] ''' print(re.findall('her:?', text)) '''
Finds all occurrences of the word 'her',
followed by zero or one occurrences of the colon ':'.
['her:', 'her', 'her'] ''' print(re.findall('her:+', text)) '''
Finds all occurrences of the word 'her',
followed by one or more occurrences of the colon ':'.
['her:'] ''' print(re.findall('^Ha.*', text)) '''
Finds all occurrences where the string starts with
the character sequence 'Ha', followed by an arbitrary
number of characters except for the new-line character. Can you figure out why Python doesn't find any?
[] ''' print(re.findall('n$', text)) '''
Finds all occurrences where the new-line character 'n'
occurs at the end of the string.
['n'] ''' print(re.findall('(Life|Death)', text)) '''
Finds all occurrences of either the word 'Life' or the
word 'Death'.
['Life', 'Death'] '''In these examples, you’ve already seen the special symbol ‘n’ which denotes the new-line character in Python (and most other languages). There are many special characters, specifically designed for regular expressions. Next, we’ll discover the most important special symbols. Related Re Methods There are seven important regular expression methods which you must master:
These seven methods are 80% of what you need to know to get started with Python’s regular expression functionality. Where to Go From Here? You’ve learned everything you need to know about the Python Regex AND Operator. Summary: There are different interpretations for the AND operator in a regular expression (regex):
Want to earn money while you learn Python? Average Python programmers earn more than $50 per hour. You can certainly become average, can’t you? Join the free webinar that shows you how to become a thriving coding business owner online! [Webinar] Become a Six-Figure Freelance Developer with Python Join us. It’s fun!
|