【问题标题】:Java - regular expression for get number formatJava - 获取数字格式的正则表达式
【发布时间】:2013-04-23 11:41:45
【问题描述】:

我有这个:

  • 110121 自然 95 1570,40
  • 110121 自然 95 1570,40*
  • 41,110 1 x 38,20 捷克克朗)[A] *
  • ' 31,831 261,791 1308,61)
  • >01572 PRAVO SO 17,00
  • 1,000 ks x 17,00
  • 1570,40

此输出的每一行都保存在 List 中,我想得到数字 1570,40

对于这种格式,我的正则表达式看起来像这样

    "([1-9][0-9]*[\\.|,][0-9]{2})[^\\.\\d](.*)"
    "^([1-9][0-9]*[\\.|,][0-9]{2})$"

我有一个问题,如果建立(通过第二个正则表达式),最后一行的 1570,40,还有 1570,40(从最后的 1570,40* 行)但第一行没有建立..你知道问题出在哪里吗?

【问题讨论】:

  • 我有点困惑。您使用的是哪个正则表达式?第一个或第二个。而且你只想找到最后一个结果,对吧?
  • 请注意,[a|b]a|b。在字符列表中,|不是替代分隔符。所以[.,] 应该是你想要的。但就我所能说的,第二个正则表达式应该可以工作。
  • 我两个都用...我有 priceFormats.add("([1-9][0-9]*[\\.|,][0-9]{2})[ ^\\.\\d](.*)");和 priceFormats.add("^([1-9][0-9]*[\\.|,][0-9]{2})$"); ...它是价格格式列表,然后是我申请的周期 (int i = 0; i
  • 您希望1570,40 在每一行中都匹配吗?你的比赛号码总是有分位数吗?
  • 是的,我希望在每一行中匹配数字...第二个正则表达式(带有 ^ 和 $)在那里,因为第一个正则表达式与单行中的数字不匹配(最后一行)。

标签: java regex string


【解决方案1】:

不确定我是否完全理解您的需求,但我认为您可以使用以下词语边界:

\b([1-9]\d*[.,]\d{2})\b

为了不匹配日期,您可以使用:

(?:^|[^.,\d])(\d+[,.]\d\d)(?:[^.,\d]|$)

解释:

The regular expression:

(?-imsx:(?:^|[^.,\d])(\d+[,.]\d\d)(?:[^.,\d]|$))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    ^                        the beginning of the string
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [^.,\d]                  any character except: '.', ',', digits
                             (0-9)
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
    [,.]                     any character of: ',', '.'
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [^.,\d]                  any character except: '.', ',', digits
                             (0-9)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

【讨论】:

  • 谢谢你,你救了我的命和工作!
  • 但仍然......例如当我有“16.10.2012”所以边界不起作用......模式返回16.10
【解决方案2】:

"([1-9][0-9]*[\\.|,][0-9]{2})[^\\.\\d](.*)"[^\\.\\d],这意味着它需要一个非数字、非点符号紧跟在数字后面。第二行有与之匹配的*。第一行在行尾有数字,所以没有匹配。我认为您只需要一个可以捕获所有数字的正则表达式:[^.\\d]*([1-9][0-9]*[.,][0-9]{2})[^.\\d]*。此外,您应该使用find 而不是match 来查找字符串中的任何子字符串,而不是匹配整个字符串。此外,如果一行中有两个这样的数字,也许找到所有匹配项是有意义的,不确定它是否适合你。

另外,使用[0-9]\d。目前它令人困惑 - 它的含义相同,但看起来不同。

【讨论】:

    【解决方案3】:

    试试这个:

    String s = "41,110 1 x 38,20 CZK)[A] * ";
    Matcher m = Pattern.compile("\\d+,\\d+").matcher(s);
    while(m.find()) {
        System.out.println(m.group());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 2015-12-29
      相关资源
      最近更新 更多