Regular Expressions 101

Community Patterns

Positive Lookbehind example

0

Regular Expression
PCRE2 (PHP >=7.3)

/
(?P<sale_items>\$.+(?<=\.99)$)
/
gm

Description

Find all numbers:

\d{1,3}(,\d{3})*(.\d{2})?

Find numbers at beginning on the line

^\d{1,3}(,\d{3})*(.\d{2})?

Find all numbers that are dollar values

$\d{1,3}(,\d{3})*(.\d{2})?

Find only numbers ending in .99

$\d{1,3}(,\d{3})*(.99)?

This catches the following (bolded for what is captured):

$14.99 $156.98 $85.99 $43.97 $1.98 $83.74 $1,601.99

Not quite right

$.*.(99)?

Also does the same as above but includes the dot for all cases

POSITIVE LOOKAHEAD:

$.+(?<=.99)$

Targets the exact thing we want:

$14.99 $156.98 $85.99 $43.97 $1.98 $83.74 $1,601.99

We can wrap in parenthesis for capturing those numbers

($.+(?<=.99)) to return capture groups

Sub with On sale: $0 to return:

On sale: $14.99 $156.98 On sale: $85.99 $43.97 $1.98 $83.74 On sale: $1,601.99

Named groups:

(?P<sale_items>$.+(?<=.99))

Sub:

On sale: $sale_items

Submitted by anonymous - 2 years ago