我有这个:

  • 110121自然95 1570,40
  • 110121 NATURAL 95 1570,40 *
  • 41,110 1 x 38,20 CZK)[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 *开始),但是没有建立第一行。是问题吗?

    最佳答案

    不确定我是否了解您的需求,但我认为您可以使用如下字词边界:

    \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
    ----------------------------------------------------------------------
    

    10-04 11:41