如何使用java为字符串编写条件逻辑

时间:2016-06-30 08:17:22

标签: java regex string conditional

我们有零售业数据。在那里,我们需要使用转换因子(即第4列)将每个单位SKU转换为CASU SKU&S; 输入数据

We have input data for 
Col1                        COL2               COL3       COL4  col5
ABHS-SMH-4OZ-01            EA               CS             12     1
ABHK-SMH-01                EA               CS             24      1

转型后的预期数据:

Col1                        COL2               COL3       COL4 col5 
ABHS-SMH-4OZ-12            EA                   CS       12     1
ABHK-SMH-24                EA                   CS       24     1

我们正在尝试用Java语言编写转换/条件逻辑。

到目前为止,我们尝试使用正则表达式:

我想搜索一些东西

e.g。 " ABHS-SMH-4OZ-01"

搜索" -01"

返回" ABHS-SMH-4OZ-24"

非常感谢任何帮助

This is my regex so far

" ABHS-SMH-4OZ-01" .matches(" -01 "。); 在此先感谢。

1 个答案:

答案 0 :(得分:2)

描述

^(?=(?:(?:(\S+))\s+){4})(\S+-)01(?=\s)

Regular expression visualization

**要更好地查看图像,只需右键单击图像并在新窗口中选择视图

此正则表达式将执行以下操作:

  • 向前看并将COL4中的值捕获到捕获组1
  • 将COL1中的主要字符与最后-01
  • 匹配
  • 使用前导字符后跟COL4
  • 中的值替换COL1中的值

实施例

现场演示

示例文字

Col1                        COL2               COL3       COL4  col5
ABHS-SMH-4OZ-01            EA               CS             12     1
ABHK-SMH-01                EA               CS             24      1

替换后

Col1                        COL2               COL3       COL4  col5
ABHS-SMH-4OZ-12            EA               CS             12     1
ABHK-SMH-24                EA               CS             24      1

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (4 times):
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        (                        group and capture to \1:
----------------------------------------------------------------------
          \S+                      non-whitespace (all but \n, \r,
                                   \t, \f, and " ") (1 or more times
                                   (matching the most amount
                                   possible))
----------------------------------------------------------------------
        )                        end of \1
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
      \s+                      whitespace (\n, \r, \t, \f, and " ")
                               (1 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
    ){4}                     end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    \S+                      non-whitespace (all but \n, \r, \t, \f,
                             and " ") (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
    -                        '-'
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  01                       '01'
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------