December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 47 | Testing a Pattern with the Positive Lookahead |
Suppose your task is to fix a mistake in several documents that inaccurately list a company’s revenue in dollars, instead of euros. You need to find all instances of “$90.3 million” and replace “$” with “€.”
So, what you need is a regex pattern that finds “$90.3 million” in a sentence such as this:
| | "The Company posted a September quarter record revenue of $90.3 million, |
| | up 8 percent year over year." |
But you want to match and replace only the currency sign, not the entire string. You need a way to exclude “90.3 million” from the match result.
Use a positive lookahead denoted by (?= ... ) to match the part of the string that you don’t want to include in the result:
Read now
Unlock full access