我有这个:
这个输出的每一行都保存在列表中,我想得到1570,40
对于这种格式,我的正则表达式如下所示
"([1-9][0-9]*[\\.|,][0-9]{2})[^\\.\\d](.*)"
"^([1-9][0-9]*[\\.|,][0-9]{2})$"我有一个问题,1570,40在最后一行如果建立(由第二个正则表达式),也1570,40 (从线1570,40*在末尾),但第一行是没有根据的。你知道问题出在哪里吗?
发布于 2013-04-23 11:56:15
我不确定我是否理解你的需要,但我认为你可以用单词的界限,比如:
\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
----------------------------------------------------------------------发布于 2013-04-23 11:54:52
试试这个:
String s = "41,110 1 x 38,20 CZK)[A] * ";
Matcher m = Pattern.compile("\\d+,\\d+").matcher(s);
while(m.find()) {
System.out.println(m.group());
}发布于 2013-04-23 11:56:35
"([1-9][0-9]*[\\.|,][0-9]{2})[^\\.\\d](.*)"有[^\\.\\d],这意味着它期望在数字后面有一个非数字的、非点的符号.第二行有与其匹配的*。第一行在行尾有数字,所以没有匹配的。我认为您只需要一个regexp就可以捕获所有的数字:[^.\\d]*([1-9][0-9]*[.,][0-9]{2})[^.\\d]*。另外,您应该使用find而不是match来查找字符串中的任何子字符串,而不是匹配整个字符串。另外,如果一条线中有两个这样的数字,它可能有找到所有匹配的意义,不确定这是否是你的情况。
另外,可以使用[0-9]或\d。目前,它令人困惑-它的意思相同,但看上去不同。
https://stackoverflow.com/questions/16168458
复制相似问题