gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:33:07 25 4
gpt4 key购买 nike

我有这个:

  • 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

此输出的每一行都保存在列表中,我想获得编号 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
----------------------------------------------------------------------

关于Java - 获取数字格式的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com