java提取只需要数字

时间:2014-04-13 20:07:01

标签: java numbers format extract

我有一个示例字符串:

Today 2014 g. and i need buy 4135 g. coca-cola and 632.35 g bread and 7,3 g. salt. Notes by 1996

备注:

  1. g 最后可能,最后可能没有点
  2. 2014 g。这是一年,格式 20xx g。 19xx g 。仅
  3. 需要:

    但我需要提取 4135 g。 632.35 g 7,3 g。仅!!!!

    如果我们找到20xx(结尾处有 g。)或19xx(结尾处有 g。) - 这就是igore! (不要提取!)

    请帮助我,请使用正则表达式字符串(对于java)

1 个答案:

答案 0 :(得分:0)

这个正则表达式可以帮到你:

\b(?!19|20)(\d+(?:\.\d+)?)\s+g\.?\b

即,在java正则表达式中:

private static final Pattern PATTERN
    = Pattern.compile("\\b(?!19|20)(\\d+(?:\\.\\d+)?)\\s+g\\.?\\b");

从您的输入中创建Matcher,与.find()一起循环,并为每场比赛提取.group(1)

final Matcher m = PATTERN.matcher(input);

while (m.find())
    System.out.println(m.group(1));

正则表达式的分解:

\b            # Find a position where we have a word limit, then
(?!19|20)     # find a position where we don't have "19" or "20" following, then
(             # begin capturing group
  \d+         # one or more digits, followed by
  (?:         # begin non capturing group
    \.\d+     # one dot, followed by one or more digits
  )?          # end none capturing group, zero or one time,
)             # end capturing group, followed by
\s+           # one or more spaces, followed by
g\.?          # "g", then a literal dot, zero or one time, followed by
\b            # a word anchor again