我有这个:
此输出的每一行都保存在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*)但是第一行没有成立...你知道吗?是问题吗?
不确定我是否充分理解您的需求,但我认为您可以使用单词边界,例如:
\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)