RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex lookahead explained

/\d+(?= ?USD)/g

A lookahead checks what follows without consuming it, so the match itself is only the number — the currency stays available for the next match and never appears in your result. This is how you extract a value that is identified by its context.

What each part does

\d+the digits you actually want
(?= ?USD)only if optionally-spaced USD comes next
the USD text is not part of the match

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
price 42 USDmatches42
100USDmatches100
7 USD and 8 EURmatches7
42 EURno match
USD 42no match

The mistake to avoid

Expecting the lookahead to appear in the match. It never does — that is the entire point, and it is why the group indexes do not shift.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers