Java - 获取数字格式的正则表达式

Jan*_*ner 6 java regex string

我有这个:

  • 110121 NATURAL 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})$"
Run Code Online (Sandbox Code Playgroud)

我有一个问题,如果最后一行是1570,40(通过第二个正则表达式),也是1570,40(最后一行是1570,40*)但是第一行没有成立...你知道吗?是问题吗?

Tot*_*oto 1

不确定我是否充分理解您的需求,但我认为您可以使用单词边界,例如:

\b([1-9]\d*[.,]\d{2})\b
Run Code Online (Sandbox Code Playgroud)

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

(?:^|[^.,\d])(\d+[,.]\d\d)(?:[^.,\d]|$)
Run Code Online (Sandbox Code Playgroud)

解释:

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
----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)