首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java外接程序只需编号

java外接程序只需编号
EN

Stack Overflow用户
提问于 2014-04-13 20:07:01
回答 1查看 29关注 0票数 0

我有一个样本字符串:

代码语言:javascript
复制
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.这是一年,可能是格式为20 or g.或19 or g。仅限

需要:

但我需要提取4135克和632.35克和7,3克只!

如果我们找到20 at (结尾有或没有g. )或19 at (结尾有或没有g. )--这就成了冰屋!(不要抽离!)

请帮助我,请为regex字符串(对于java)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-13 20:11:15

这个regex可以帮你做到这一点:

代码语言:javascript
复制
\b(?!19|20)(\d+(?:\.\d+)?)\s+g\.?\b

Ie,在java正则表达式中:

代码语言:javascript
复制
private static final Pattern PATTERN
    = Pattern.compile("\\b(?!19|20)(\\d+(?:\\.\\d+)?)\\s+g\\.?\\b");

从输入中创建一个Matcher,使用.find()循环并为每个匹配提取.group(1)

代码语言:javascript
复制
final Matcher m = PATTERN.matcher(input);

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

正则表达式的分解:

代码语言:javascript
复制
\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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23047811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档