Python (re)

Negative Lookbehind (Decimals Without $) in PY

Use negative lookbehind `(?<!...)` to match decimal numbers NOT preceded by a dollar sign.

Try it in the PY tester →

Pattern

regexPY
(?<!\$)\b\d+\.\d{2}\b   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"(?<!\$)\b\d+\.\d{2}\b")
input_text = "Price $19.99 vs ratio 1.50"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

(?<!\$) is a zero-width assertion that succeeds only when the position is NOT preceded by `$`. \b\d+\.\d{2}\b then matches a decimal with exactly two fractional digits. JS and Python both support negative lookbehind; Go's RE2 does NOT support any lookbehind at all.

Examples

Input

Price $19.99 vs ratio 1.50

Matches

  • 1.50

Input

Discount 10.00 off $99.99

Matches

  • 10.00

Input

$5.00 only

No match

Same pattern, other engines

← Back to Negative Lookbehind (Decimals Without $) overview (all engines)