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.
| Input | Result | Matched |
|---|---|---|
| price 42 USD | matches | 42 |
| 100USD | matches | 100 |
| 7 USD and 8 EUR | matches | 7 |
| 42 EUR | no match | — |
| USD 42 | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID