Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

18 Python Lookbehind

The document explains Python lookbehind, including positive and negative lookbehind. Positive lookbehind matches a position if the regex inside can be matched ending at that position, while negative lookbehind matches if the regex inside cannot be matched ending at that position. Examples are provided to demonstrate matching words preceded or not preceded by other words using positive and negative lookbehind.

Uploaded by

ArvindSharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

18 Python Lookbehind

The document explains Python lookbehind, including positive and negative lookbehind. Positive lookbehind matches a position if the regex inside can be matched ending at that position, while negative lookbehind matches if the regex inside cannot be matched ending at that position. Examples are provided to demonstrate matching words preceded or not preceded by other words using positive and negative lookbehind.

Uploaded by

ArvindSharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Lookbehind

Python Lookbehind explained.

WE'LL COVER THE FOLLOWING

• Positive Lookbehind
• Example
• Neagative Lookbehind
• Example

Positive Lookbehind #
(?<=regex) Matches at a position if the pattern inside the lookbehind can be
matched ending at that position.

Example #
Consider the following string:

begin:learner1:scientific:learner2:scientific:learner3:end

Positive lookbehind assertion can help us to find all words 'scientific' ,


'scientific' and 'end' preeceded by the words learner{1-3} .

import re

string = "begin:learner1:scientific:learner2:scientific:learner3:end"
print re.findall(r"(?<=learner\d:)(\b\w*\b)", string)

Neagative Lookbehind #
Similar to positive lookbehind, (?<!regex) matches at a position if the pattern
inside the lookbehind cannot be matched ending at that position.

Example #
Let’s now proceed to an example, where we find the word ( begin ), not
preceded by the words learner{1-3} .

import re

string = "begin:learner1:scientific:learner2:scientific:learner3:end"
print re.findall(r"^(?<!learner\d:)(\b\w*\b)", string)

You might also like