Reputation: 747
I have the following regular expression:
^((R|AED|AFN|ALL|AMD|ANG|AOA|ARS|AUD|AWG|AZN|BAM|BBD|BDT|BGN|BHD|BIF|BMD|BND|BOB|BRL|BSD|BTN|BWP|BYR|BZD|CAD|CDF|CHF|CLP|CNY|COP|CRC|CUC|CUP|CVE|CZK|DJF|DKK|DOP|DZD|EGP|ERN|ETB|EUR|FJD|FKP|GBP|GEL|GHS|GIP|GMD|GNF|GTQ|GYD|HKD|HNL|HRK|HTG|HUF|IDR|ILS|INR|IQD|IRR|ISK|JMD|JOD|JPY|KES|KGS|KHR|KMF|KPW|KRW|KWD|KYD|KZT|LAK|LBP|LKR|LRD|LSL|LYD|MAD|MDL|MGA|MKD|MMK|MNT|MOP|MRO|MUR|MVR|MWK|MXN|MYR|MZN|NAD|NGN|NIO|NOK|NPR|NZD|OMR|PAB|PEN|PGK|PHP|PKR|PLN|PYG|QAR|RON|RSD|RUB|RWF|SAR|SBD|SCR|SDG|SEK|SGD|SHP|SLL|SOS|SRD|SSP|STD|SYP|SZL|THB|TJS|TMT|TND|TOP|TRY|TTD|TWD|TZS|UAH|UGX|USD|UYU|UZS|VEF|VND|VUV|WST|XAF|XCD|XOF|XPF|YER|ZAR|ZMW) ?([1-9][0-9]{0,2}((,| )?[0-9]{3}){2,}|0)\.[0-9][0-9])?$
It will match on any Currency value 1,000,000 or greater. I need it to match on 5,000,000 or greater. That seems like it should be a simple change but I'm struggling with it.
Thanks for the help.
Upvotes: 1
Views: 669
Reputation: 15010
^(R|AED|AFN|ALL|AMD|ANG|AOA|ARS|AUD|AWG|AZN|BAM|BBD|BDT|BGN|BHD|BIF|BMD|BND|BOB|BRL|BSD|BTN|BWP|BYR|BZD|CAD|CDF|CHF|CLP|CNY|COP|CRC|CUC|CUP|CVE|CZK|DJF|DKK|DOP|DZD|EGP|ERN|ETB|EUR|FJD|FKP|GBP|GEL|GHS|GIP|GMD|GNF|GTQ|GYD|HKD|HNL|HRK|HTG|HUF|IDR|ILS|INR|IQD|IRR|ISK|JMD|JOD|JPY|KES|KGS|KHR|KMF|KPW|KRW|KWD|KYD|KZT|LAK|LBP|LKR|LRD|LSL|LYD|MAD|MDL|MGA|MKD|MMK|MNT|MOP|MRO|MUR|MVR|MWK|MXN|MYR|MZN|NAD|NGN|NIO|NOK|NPR|NZD|OMR|PAB|PEN|PGK|PHP|PKR|PLN|PYG|QAR|RON|RSD|RUB|RWF|SAR|SBD|SCR|SDG|SEK|SGD|SHP|SLL|SOS|SRD|SSP|STD|SYP|SZL|THB|TJS|TMT|TND|TOP|TRY|TTD|TWD|TZS|UAH|UGX|USD|UYU|UZS|VEF|VND|VUV|WST|XAF|XCD|XOF|XPF|YER|ZAR|ZMW)\s?([5-9][,\s][0-9]{3}[,\s]|[0-9]{2,3}[,\s][0-9]{3}[,\s]|[0-9]{1,3}[,\s](?:[0-9]{3}[,\s]){2,})
([0-9]{3})\.([0-9]{2})$
This regular expression will do the following:
Live Demo
https://regex101.com/r/cS7aA5/1
Sample text
usd 5,000,000.00
usd 15,000,000.00
usd 15,000,000,000.00
usd 15,000,000,000,000.00
usd 5,000.00
usd 4,999,999.99
usd
Sample Matches
usd 5,000,000.00
usd 15,000,000.00
usd 15,000,000,000.00
usd 15,000,000,000,000.00
NODE EXPLANATION
^ the beginning of a "line"
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
R 'R'
----------------------------------------------------------------------
| OR
All the other money types
| OR
----------------------------------------------------------------------
ZMW 'ZMW'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\s? whitespace (\n, \r, \t, \f, and " ")
(optional (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[5-9] any character of: '5' to '9'
----------------------------------------------------------------------
[,\s] any character of: ',', whitespace (\n,
\r, \t, \f, and " ")
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
[,\s] any character of: ',', whitespace (\n,
\r, \t, \f, and " ")
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[0-9]{2,3} any character of: '0' to '9' (between 2
and 3 times (matching the most amount
possible))
----------------------------------------------------------------------
[,\s] any character of: ',', whitespace (\n,
\r, \t, \f, and " ")
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
[,\s] any character of: ',', whitespace (\n,
\r, \t, \f, and " ")
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[0-9]{1,3} any character of: '0' to '9' (between 1
and 3 times (matching the most amount
possible))
----------------------------------------------------------------------
[,\s] any character of: ',', whitespace (\n,
\r, \t, \f, and " ")
----------------------------------------------------------------------
(?: group, but do not capture (at least 2
times (matching the most amount
possible)):
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
[,\s] any character of: ',', whitespace (\n,
\r, \t, \f, and " ")
----------------------------------------------------------------------
){2,} end of grouping
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
( group and capture to \4:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \4
----------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
----------------------------------------------------------------------
You can further improve your expression execution time by 66% by replacing the many currency alternations with a simple span tree. The above expression takes 1060 steps to complete on that sample text, whereas the following expression takes 324 steps to complete.
^(A(?:ED|FN|LL|MD|NG|OA|RS|UD|WG|ZN)|B(?:AM|BD|DT|GN|HD|IF|MD|ND|OB|RL|SD|TN|WP|YR|ZD)|C(?:AD|DF|HF|LP|NY|OP|RC|UC|UP|VE|ZK)|D(?:JF|KK|OP|ZD)|E(?:GP|RN|TB|UR)|F(?:JD|KP)|G(?:BP|EL|HS|IP|MD|NF|TQ|YD)|H(?:KD|NL|RK|TG|UF)|I(?:DR|LS|NR|QD|RR|SK)|J(?:MD|OD|PY)|K(?:ES|GS|HR|MF|PW|RW|WD|YD|ZT)|L(?:AK|BP|KR|RD|SL|YD)|M(?:AD|DL|GA|KD|MK|NT|OP|RO|UR|VR|WK|XN|YR|ZN)|N(?:AD|GN|IO|OK|PR|ZD)|OMR|P(?:AB|EN|GK|HP|KR|LN|YG)|QAR|R(?:ON|SD|UB|WF)?|S(?:AR|BD|CR|DG|EK|GD|HP|LL|OS|RD|SP|TD|YP|ZL)|T(?:HB|JS|MT|ND|OP|RY|TD|WD|ZS)|U(?:AH|GX|SD|YU|ZS)|V(?:EF|ND|UV)|WST|X(?:AF|CD|OF|PF)|YER|Z(?:AR|MW))\s?([5-9][,\s][0-9]{3}[,\s]|[0-9]{2,3}[,\s][0-9]{3}[,\s]|[0-9]{1,3}[,\s](?:[0-9]{3}[,\s]){2,})([0-9]{3})\.([0-9]{2})$
Upvotes: 3
Reputation: 12045
The segment of your regular expression that needs to be addressed are the character classes that match a range from 1 to 999:
[1-9][0-9]{0,2}
Use the alternation token |
in conjunction with an increase to the first argument of the quantifier {}
to achieve a range from 5 to 999:
Optional enhancements:
For brevity, you may also like to modify the leading currency identifier set to a character class instead:
(R|[A-Z]{3})
Likewise, you can minimize the segment that matches the trailing two-digit decimal with a quantifier:
\.[0-9]{2}
Putting it all together:
((R|[A-Z]{3}) ?([5-9]|[1-9][0-9]{1,2})((,| )?[0-9]{3}){2,})\.[0-9]{2}
Source: Regexper.com
Upvotes: 0
Reputation: 6357
Use this regex.
^((R|AED|AFN|ALL|AMD|ANG|AOA|ARS|AUD|AWG|AZN|BAM|BBD|BDT|BGN|BHD|BIF|BMD|BND|BOB|BRL|BSD|BTN|BWP|BYR|BZD|CAD|CDF|CHF|CLP|CNY|COP|CRC|CUC|CUP|CVE|CZK|DJF|DKK|DOP|DZD|EGP|ERN|ETB|EUR|FJD|FKP|GBP|GEL|GHS|GIP|GMD|GNF|GTQ|GYD|HKD|HNL|HRK|HTG|HUF|IDR|ILS|INR|IQD|IRR|ISK|JMD|JOD|JPY|KES|KGS|KHR|KMF|KPW|KRW|KWD|KYD|KZT|LAK|LBP|LKR|LRD|LSL|LYD|MAD|MDL|MGA|MKD|MMK|MNT|MOP|MRO|MUR|MVR|MWK|MXN|MYR|MZN|NAD|NGN|NIO|NOK|NPR|NZD|OMR|PAB|PEN|PGK|PHP|PKR|PLN|PYG|QAR|RON|RSD|RUB|RWF|SAR|SBD|SCR|SDG|SEK|SGD|SHP|SLL|SOS|SRD|SSP|STD|SYP|SZL|THB|TJS|TMT|TND|TOP|TRY|TTD|TWD|TZS|UAH|UGX|USD|UYU|UZS|VEF|VND|VUV|WST|XAF|XCD|XOF|XPF|YER|ZAR|ZMW) ?(([1-9][0-9][0-9]|[1-9][0-9]|[5-9])((,| )?[0-9]{3}){2,}|0)\.[0-9][0-9])?$
Updated link.
Upvotes: 0
Reputation: 23958
Keep your regex as it is.
preg_match($patter, $string, $out);
$amount = str_replace(",","", $out[2]);
If ($amount>=5000000){
// code here
}
Upvotes: -1