简体   繁体   English

正则表达式问题

[英]Regular expression woes

I have the following data of which i would like to convert it to regular expression. 我有以下数据,我想将其转换为正则表达式。

PALMKERNEL OIL Mal/Indo dlrs tonne cif Rotterdam 棕榈油Mal / Indo dlrs吨CIF鹿特丹

Dec15/Jan16 890.00 12月15日/ 1月16日890.00

Jan16/Feb16 900.00 +10.00 1月16日/ 2月16日900.00 +10.00

My code below doesn't seem to work. 我的以下代码似乎无效。 Firstly how do I determine that after the 890, there could be either nothing or there could be a +10.00 or any number in that format? 首先,我如何确定890之后的格式是否为空,或者为+10.00或任何数字? I tried to use ?: but sometimes it will totally ignore the month information which i am trying to capture..In this case i do not want to capture the +10.00 or any characters after the price of 890 or 900. 我尝试使用?:但有时它会完全忽略我要捕获的月份信息。在这种情况下,我不想捕获+10.00或890或900价格后的任何字符。

(PALMKERNEL OIL Mal\\b)/(Indo dlrs tonne cif Rotterdam\\b)\\s*([^\\s]+)\\s*(\\d*.?\\d*)\\s*([^\\s]+|[+\\d*.?\\d*])\\s*(\\d*.?\\d*)\\s*([^\\s]+|(?:[+\\d*.?\\d*])) (棕榈油Mal \\ b)/(印度dlrs吨到岸价鹿特丹\\ b)\\ s *([^ \\ s] +)\\ s *(\\ d *。?\\ d *)\\ s *([^ \\ s] + | [+ \\ d *。?\\ d *])\\ s *(\\ d *。?\\ d *)\\ s *([^ \\ s] + |(?:[+ \\ d *。?\\ d *]))

For the part with the dates and prices, this regular expression handles the two variants in your sample string. 对于带有日期和价格的部分,此正则表达式处理示例字符串中的两个变体。

Pattern pat = Pattern.compile( 
     "\\w{3}\\d{1,2}/\\w{3}\\d{1,2}" +
     "\\s*(\\d+\\.\\d\\d)(\\s+\\+\\d+\\.\\d\\d)?" );
String s1 = "Dec15/Jan16 890.00";
String s2 = "Jan16/Feb16 900.00 +10.00";
Matcher m1 = pat.matcher( s1 ); 
if( m1.matches() )
  System.out.println("m1 " + m1.group(1) + ":" + m1.group(2) );
Matcher m2 = pat.matcher( s2 ); 
if( m2.matches() )
  System.out.println("m2 " + m2.group(1) + ":" + m2.group(2) );

Output: 输出:

m1 890.00:null
m2 900.00: +10.00

There's not enough information - so I don't know about a third alternative in /+10.00/?. 没有足够的信息-所以我不知道/+10.00/?中的第三种选择。

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

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