USD Currency (Inline) in GO
Match US dollar amounts inline in text: `$1,234.56`, `$99`, `$1,000,000.00`.
Try it in the GO tester →Pattern
regexGO
\$\d{1,3}(?:,\d{3})*(?:\.\d{2})? (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\$\d{1,3}(?:,\d{3})*(?:\.\d{2})?`)
input := `Total $1,234.56 plus tax of $99`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
\$ matches the literal dollar sign. \d{1,3} matches the first 1–3 digits. (?:,\d{3})* matches additional thousands groups (comma + 3 digits). (?:\.\d{2})? optionally matches a decimal portion with exactly two digits (cents).
Examples
Input
Total $1,234.56 plus tax of $99Matches
$1,234.56$99
Input
Bonus $1,000,000.00 awardedMatches
$1,000,000.00
Input
no money mentionedNo match
—