我有一个样本字符串:
Today 2014 g. and i need buy 4135 g. coca-cola and 632.35 g bread and 7,3 g. salt. Notes by 1996备注:
需要:
但我需要提取4135克和632.35克和7,3克只!
如果我们找到20 at (结尾有或没有g. )或19 at (结尾有或没有g. )--这就成了冰屋!(不要抽离!)
请帮助我,请为regex字符串(对于java)
发布于 2014-04-13 20:11:15
这个regex可以帮你做到这一点:
\b(?!19|20)(\d+(?:\.\d+)?)\s+g\.?\bIe,在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 againhttps://stackoverflow.com/questions/23047811
复制相似问题