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

标签 java regex string conditional-statements

我们有零售行业数据。其中,我们需要使用转换系数(即第 4 列)将每个单位 SKU 转换为案例 SKU 输入数据

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 语言编写转换/条件逻辑。

到目前为止,我们尝试了以下正则表达式:

我想搜索一些东西

例如“ABHS-SMH-4OZ-01”

搜索“-01”

返回“ABHS-SMH-4OZ-24”

任何帮助将不胜感激

This is my regex so far

"ABHS-SMH-4OZ-01".matches(".-01."); 提前致谢。

最佳答案

描述

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

Regular expression visualization

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

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

  • 向前看并将 COL4 中的值捕获到捕获组 1
  • 匹配 COL1 中的前导字符直到最后一个 -01
  • 将 COL1 中的值替换为前导字符,后跟 COL4 中的值

示例

现场演示

示例文本

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
----------------------------------------------------------------------

关于java - 如何使用java编写字符串的条件逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38117562/

相关文章:

java - Spring JSON url 模式 Not Acceptable

android - 如何将数据传递给广播接收器?

c - Strlwr 函数 - 在 xcode 9.2 中出现错误

java - 用 .replaceAll 替换特殊字符

java - JPanel 中使用 Miglayout 的多个 JTable

java - ListView 中的 NullPointErexception

java - G1 垃圾收集器是否对每个区域使用相同的区域大小?

PHP Regex - 删除多余的空格但保留换行符

Java正则表达式用符号替换新行

java - 队列没有产生正确的输出