https://www.regexroo.com

Lesson 21: Positive Lookbehind in Regex

Following our exploration of lookahead, we delve into another advanced feature of regular expressions: positive lookbehind. This tool offers a mirror image of lookahead, focusing on sequences that precede a given match rather than those that follow it. The aim of this lesson is to provide a comprehensive understanding of positive lookbehind, its applications, and how it can enhance your regex expertise.

Defining Positive Lookbehind

Positive lookbehind allows you to assert that a certain pattern is present right before the current position in the string. However, just like lookahead, lookbehind doesn't consume characters. This means it checks for the presence of a pattern without including it in the final match. The syntax for a positive lookbehind is (?<=...), where the ellipsis (...) is the pattern you're checking for.

Practical Applications of Positive Lookbehind

Positive lookbehinds find extensive use in scenarios where you wish to match a sequence only if it's preceded by a specific pattern. Whether you're extracting specific content from logs, data processing, or performing intricate string manipulations, positive lookbehind proves essential. For example, if you need to match currency amounts without the currency symbol, but only when they are preceded by a "$", positive lookbehind is your tool of choice.

Limitations and Considerations

Although powerful, not all regex engines support positive lookbehinds. Additionally, some engines impose restrictions on the patterns you can use within a lookbehind, like fixed-width patterns. It's crucial to be aware of these nuances to ensure portability and performance across various platforms.

Examples of Positive Lookbehind in Action

1. Extracting amounts following a dollar sign.

Given the string:  He owes me $50 for the concert ticket.  Using the regex (?<=\$)\d+, we would capture "50" as it is preceded by a "$", without including the "$" in our match.

2. Identifying words after "Dear".

If we're interested in names that directly follow the word "Dear" in a letter, consider the string:  Dear John,  The regex (?<=Dear\s)\w+ would match "John" as it is the word immediately following "Dear".

Exercise 21: Mastering Positive Lookbehind

For this practice exam, we'll harness the power of positive lookbehind to extract specific sequences from a set of strings. Your task is to craft a regular expression pattern that matches captures monetary amounts that are immediately preceeded by a the EUR or USD currency symbols.