QUESTION

Text
Image

Regular Expression for Salaries.

How is the Python regular expression look like for this question? + Detailed explanation, thanks.


After successfully implementing the famous FizzBuzz task, Jeffrey started waiting for a job offer from a big software company. As he is very serious about his employment, the only kinds of messages that he wants to read are exact salary proposals payed in: - US Dollars (USD) - Canadian Dollars (CAD) - or Great British Pounds (GBP). He needs your help in filtering the messages that he is interested in. Help him by writing an appropriate regular expression. The message should consist of: - currency abbreviation ("USD", "CAD" or "GBP"), - exactly one space character, - the non-negative value in the format described below. The value can be an integer (e.g. 10) or it can have a fractional part, in which case we allow exactly two digits after the dot (e.g. 0.10). Additionally, the integer part must be in a convenient format: starting from the end, each group of three digits must be separated with a comma (e.g. 1,123,412). Leading zeros in the integer part are not allowed. Example test cases: "GBP $1,123 "$ "USD $0.1 \theta^{\prime \prime}$ "GBP $1,123,412 "$ "CAD $10.00 "$ "CAD 10" "ABC 10" "aUSD 10" should not match (too many spaces) should match should match should match should match should not match (wrong currency) should not match (redundant characters
Example test cases: "GBP $1,123 "$ "USD $0.10 "$ "GBP $1,123,412 "$ "CAD $10.00 "$ "CAD $10 "$ "ABC $10 "$ "qUSD 10" at the beginning) "USD 10q" at the end) "CAD 10.1231" long) "USD $012.12 "$ zero) should not match (too many spaces) should match should match should match should match should not match (wrong currency) should not match (redundant characters should not match (redundant characters should not match (fractional part too should not match (redundant leading You can assume the input string will not contain newlines. Your regular expression will be evaluated using Python's re . search. Do not write any code other than the expression; for example, to match a string containing a digit, your solution should be $\backslash \mathrm{d}$ (without quotes or surrounding the expression by slashes).

Public Answer

EBAANJ The First Answerer