Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
(?(DEFINE) (?<currency_symbol> \p{Sc} ) (?<leading_group> [1-9] \d{0,2}+ ) (?<group> \d{3} ) (?<non_leading_groups> (?&group) (?:(?&group_delim)(?&group))*+ ) (?<decimal_delim> (?# Use the opposite of group_delim. ) (?(group_delim_comma) \. | (?(group_delim_period) , | [,.] (?# There's no definitive grouping_delim. ) ) ) ) ) ^ (?> (?&currency_symbol) (?&leading_group) (?: (?<group_delim> (?<group_delim_comma> , ) | (?<group_delim_period> \. ) | \ (?# Allow whitespace as a delimiter. ) ) (?&non_leading_groups) | \d*+ (?# It's also possible that there's no delimiter. ) ) ) (?: (?&decimal_delim) \d*+ )?+ $
/
guxm

Description

Matches typical currency input in most LTR locales. Doesn't handle ISO-style currency abbreviations. Currency symbol must be on the left side for consistency; this my conflict with conventions for some currencies. Requested by Mr Lucky at https://xenforo.com/community/threads/help-with-a-currency-regular-expression-please.104164/

Submitted by Paul Buonopane (Zenexer) - 9 years ago