gpt4 book ai didi

java - 使用可选的逗号和点匹配数字

转载 作者:行者123 更新时间:2023-12-01 17:10:00 24 4
gpt4 key购买 nike

什么是正则表达式来匹配一个或多个数字,以逗号作为千位分隔符,后跟可选的点和小数,如下所示:

必须匹配

1 
12
123,123
123,123.000
123.123

但不是,

123.123,123

最佳答案

在我看来,您正在寻找类似的东西(请参阅 demo ):

^\d+(?:,\d{3})*(?:\.\d{3})?$
  1. 这允许多个以逗号分隔的千组,如 12,345,678.000
  2. 请注意,句点后有三位数字(或没有)。如果您想允许一个或多个,请将 \d{3} 更改为 \d+

解释正则表达式

^                        # the beginning of the string
\d+ # digits (0-9) (1 or more times (matching
# the most amount possible))
(?: # group, but do not capture (0 or more times
# (matching the most amount possible)):
, # ','
\d{3} # digits (0-9) (3 times)
)* # end of grouping
(?: # group, but do not capture (optional
# (matching the most amount possible)):
\. # '.'
\d{3} # digits (0-9) (3 times)
)? # end of grouping
$ # before an optional \n, and the end of the
# string

关于java - 使用可选的逗号和点匹配数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24182227/

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