使用正则表达式验证数字和货币

时间:2017-06-15 16:43:33

标签: java regex currency

我正在研究正则表达式,但不知道该做些什么。 这些是我的正则表达式:
1){ "created_by": "Administrator IIS", "modified_on": "2017-05-29T13:09:42Z", "_context": [], "created_on": "2017-04-17T09:50:03Z", "terms": { "items": [{ "_type": "term", "_id": "6662c0f2.e1b1ec6c.1mdlmv35e.1618tt1.8mb4gb.nahef1pustcmu5k10omdh", "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.e1b1ec6c.1mdlmv35e.1618tt1.8mb4gb.nahef1pustcmu5k10omdh", "_name": "Export_01" }, { "_type": "term", "_id": "6662c0f2.e1b1ec6c.1mdln1e8h.0ibdeqc.s1nm3u.vbt90ta1civ5asp5qetu5", "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.e1b1ec6c.1mdln1e8h.0ibdeqc.s1nm3u.vbt90ta1civ5asp5qetu5", "_name": "Export_02" }, { "_type": "term", "_id": "6662c0f2.e1b1ec6c.1mdln7r4n.tn8gjjo.rd5uri.25i83fvkbo5l4bhscd3dr", "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.e1b1ec6c.1mdln7r4n.tn8gjjo.rd5uri.25i83fvkbo5l4bhscd3dr", "_name": "Mule Asset 01" }, { "_type": "term", "_id": "6662c0f2.e1b1ec6c.1mdln7rs7.3tg971k.ghjkja.lnc3bpu3h6pst1tik15ij", "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.e1b1ec6c.1mdln7rs7.3tg971k.ghjkja.lnc3bpu3h6pst1tik15ij", "_name": "Mule Asset 02" }], "paging": { "numTotal": 4, "pageSize": 10, "end": 3, "begin": 0 } }, "_name": "DGC2IGC", "_type": "category", "_id": "6662c0f2.ee6a64fe.1mdlmv34b.anfhlbg.dmjrjk.5ct1qigk0ndbkd22sqp8i", "short_description": "This is for Mulesoft Data Movement", "modified_by": "Administrator IIS", "_url": "https://10.11.13.155:9445/ibm/iis/igc-rest/v1/assets/6662c0f2.ee6a64fe.1mdlmv34b.anfhlbg.dmjrjk.5ct1qigk0ndbkd22sqp8i", "name": "DGC2IGC", "development_log": { "items": [{ "person": "Administrator IIS", "new_state": "Synched", "workflow_task": "NEW", "user_id": "isadmin", "comment": "Public comment", "date": "2017-04-17T09:57:19Z", "activity": "PUBLISHED" }, { "person": "Administrator IIS", "new_state": "Approved", "workflow_task": "NEW", "user_id": "isadmin", "comment": "Approved!", "date": "2017-04-17T09:56:48Z", "activity": "Approved" }, { "person": "Administrator IIS", "new_state": "Pending Approval", "workflow_task": "NEW", "user_id": "isadmin", "comment": "Approve!", "date": "2017-04-17T09:56:20Z", "activity": "SentForApproval" }, { "person": "Administrator IIS", "new_state": "Draft", "workflow_task": "NEW", "user_id": "isadmin", "comment": "Comment01", "date": "2017-04-17T09:50:03Z", "activity": "NEW" }], "paging": { "numTotal": 4, "beginIndex": 0, "endIndex": 3, "pageSize": 4 } }
2)"^-?\\d{1,3}(?:\\.\\d{3})*(?:,\\d+)?\\s$";
3)"^-?\\d{1,3}(?:\\.\\d{3})*(?:,\\d+)?\\s\\€$"
4)"[0-9.,]*"

这些正则表达式检查货币字符串或数字。 每一个例子 1)123.456,99
2)123.456,99€ 3)12,19
4)12,19€

每个正则表达式都适用于相关示例。 是否可以将所有正则表达式合并为一个? 如何使这个正则表达式灵活地适应该字符串中任何位置的空格。

事先提前

萨米

2 个答案:

答案 0 :(得分:2)

您可以使用^-?(\d{1,3}\s*?([.,]|$|\\s)\\s*?)+€?$

  • ^-?(\\d{1,3}\\s*?([.,]|$|\\s)\\s*?)+€?$^

    开头
    • (\\d{1,3}\s*?([.,]|$|\\s)\\s*?)+匹配一对或多对数字,由.,
    • 分隔
    • \\d{1,3}\\s*?匹配1到3之间的数字,尽可能少地匹配空格
    • ([.,]|$|\s)\\s*?零个或一个匹配.,或结束或空格,\\s*?零个或多个空格
  • €?$ 个字符的零或一个匹配,$匹配结束

Java Demo

答案 1 :(得分:0)

这应该可以解决问题

\d{1,3}(?:.|,)+€?

对于java,它应该是"\\d{1,3}(?:.|,)+€?"

我强烈建议http://regexr.com满足您的所有正则表达式需求。他们有一个很棒的“cheatsheet”和一个不错的测试环境