用于货币格式的正则表达式-Java

时间:2019-04-24 05:52:29

标签: java regex currency-formatting

我想将过滤器添加到我的async def main(): conn = sqlite3.connect("projects.db") c = conn.cursor() c.execute("SELECT * FROM memo") data=c.fetchall() while True: for task in data: if task[3]!= None: # print(type(Dt[3])) # print(Dt[3]) current_date=str(datetime.datetime.now().replace(microsecond=0)) # print(type(current_date)) #print(current_date) if(task[3]==current_date): #Put the Pop-Up Here print("Yes") await asyncio.sleep(5) async def m_main(): ev1 = loop.create_task(main()) def reminder_exit(): global newwin #loop = asyncio.get_event_loop() #try: #loop.run_until_complete(main()) m_main() #newwin.quit() #finally: #loop.close() ,并接受不同的货币值,例如

美国货币格式: 123,456.00

西班牙货币格式: 123.456,00

我还想保留小数点前最多10位,小数点后最多2位。

我用于过滤EditText值的正则表达式为EditText

但是此正则表达式接受(([0-9|(,.)]{0,13})?)?((,.)[0-9]{0,2})?,,,,,,,之类的值

如何更改严格接受具有相同模式的两种货币格式的正则表达式?

感谢您的帮助。先感谢您。

2 个答案:

答案 0 :(得分:2)

您的图案可以匹配重复的点或重复的逗号,这仅是因为由于问号,所有部分都是可选的。它也可以匹配一个空字符串。

您可以将alternation与重复组一起使用,该重复组以点或逗号开头,后跟3或2位数字,以防止出现连续的点和逗号:

说明

^(?:(?![,0-9]{14})\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?|(?![.0-9]{14})\d{1,3}(?:\.\d{3})*(?:\,\d{1,2})?)$
  • ^字符串的开头
  • (?:非捕获组
    • (?![,0-9]{14})负向超前,断言不要重复14次逗号或数字
    • \d{1,3}(?:,\d{3})*(?:\.\d{1,2})?匹配1-3位数字,重复0+次以逗号开头,后跟3位数字,或者匹配点和1-2位数字
    • |
    • (?![.0-9]{14})负向查找,断言不要重复12次点或数字
    • \d{1,3}(?:\.\d{3})*(?:\,\d{1,2})?匹配1-3位数字,重复0+次匹配点后跟3位数字,可选匹配逗号和1-2位数字
  • )关闭非捕获组
  • $声明字符串结尾

Regex demo

答案 1 :(得分:1)

NumberFormat's getCurrencyInstance方法具有Locale参数。这是处理货币格式问题的标准方法。