简体   繁体   English

Java正则表达式从日期较大的字符串中排除特定的权重

[英]java regex to exclude specific weight from a larger string with date

I have some string 我有一些线

"Today 31.12.2014g we receive goods. These weight is 31.12g (23.03.2014)" “今天我们收到的商品为31.12.2014g。这些重量为31.12g(2014年3月23日)”

31.12.2014 g - its not mistake. 2014年12月31日g-这不是错误。 Some text with date label have g letter (without space) 一些带有日期标签的文本带有g字母(无空格)

I need extract from string only weight value (without date value), but my regex: 我只需要从字符串中提取权重值(没有日期值),但是我的正则表达式:

[0-9]+\\.[0-9]+g

exctact date too :( 也是约会日期:(

my results (two group): 我的结果(两组):

12.2014g 12.2014克

31.12g <- i am need only this!!! 31.12g <-我只需要这个!!!

You can add negative look behind to make sure that before part you are interested in there is nothing you don't want which in your case seems to be 您可以在后面添加否定的外观,以确保在您感兴趣的部分之前没有您不想要的东西

  • lets say between 1 and 10 numbers with dot after it like in case 让我们说出1到10之间的数字,然后加上点号

     31.12.2014g ^^^ 
  • also to make sure that we will match entire value and not just part of it like in case 还要确保我们将匹配整个值,而不仅仅是部分匹配,以防万一

     31.12.2014g ^^^^^^^ 

    where 2.2014g fulfils condition of previous negative look behind we need to make sure that matched part should not have any digit before it 2.2014g满足先前负面观察的条件的情况下,我们需要确保匹配的部分之前没有任何数字

So try maybe something like 所以尝试像

(?<!\\d{1,10}\\.)(?<!\\d)\\d+\\.\\d+g

BTW \\d (which in Java is written as "\\\\d" ) represents [0-9] . BTW \\d (在Java中写为"\\\\d" )表示[0-9] You can change it back if you want. 您可以根据需要将其更改回去。

Demo: 演示:

String data = "Today 31.12.2014g we receive goods. These weight is 31.12g (23.03.2014)";
Pattern p = Pattern.compile("(?<!\\d{1,10}\\.)(?<!\\d)\\d+\\.\\d+g");
Matcher m = p.matcher(data);
while(m.find())
    System.out.println(m.group());

Output: 31.12g 产量: 31.12g

You could search for white spaces: 您可以搜索空格:

\\s[0-9]+\\.[0-9]+g // -> " 31.12g"

… always assume there are exactly two decimal positions: …始终假设精确地有两个小数位:

[^\\.][0-9]+\\.[0-9]{2}g // -> "31.12g" (though, this will fail if the date is spelled DD.MM.YYg) [^\\.][0-9]+\\.[0-9]{2}g // -> "31.12g" (不过,如果日期拼写为DD.MM.YYg,这将失败。)

… or work with the date: …或使用日期:

[0-9]?[0-9]\\.[0-9]?[0-9]\\.[0-9]?[0-9]?[0-9][0-9]g?.+(\\b[0-9]+\\.[0-9]+g) // -> "31.12.2014g we receive goods. These weight is 31.12g", "31.12g"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM