1

We have retail industry data . In that ,we need to convert each Unit SKU'S TO CASE SKU'S BY using conversion factor(that is column 4) Input data

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

Expected data after transformation :

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

We are trying to write the transformation/conditional logic in Java language .

We tried following regex so far:

I want to search for something

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

search for "-01"

return "ABHS-SMH-4OZ-24"

Any help would be much appreciated

This is my regex so far

"ABHS-SMH-4OZ-01".matches(".-01."); Thanks In advance.

NEO
  • 389
  • 8
  • 31
  • Yes, there are plenty ways of doing it. If you can spend of bit of time, searching the answer yourself, it will be a good learning experience for you. You can help others by posting the answer here. – shazinltc Jun 30 '16 at 08:21
  • What have you tried so far? We are here to help you when you get stuck, not to just solve your problem for you – Mshnik Jun 30 '16 at 08:22
  • I tried almost from my end but not able to do that. than after my research i posted here. – NEO Jun 30 '16 at 08:23
  • We are here to help and solve the problem also. It depends what is the requirement. If you don't like any question, then please don't demotivate. In my view, if we know the answer we should answer, else we should not dislike the question. – Ashu Phaugat Jun 30 '16 at 08:26
  • I tried to figure out this link But not sure it is relevant to above :http://stackoverflow.com/questions/537174/putting-char-into-a-java-string-for-each-n-characters – NEO Jun 30 '16 at 08:31
  • You can use this pattern: `(?<=[A-Z]{4}-[A-Z]{3}(-[A-Z0-9]{3})?-)([0-9]{2})`. And then replace with the value of column 4. – Maria Ivanova Jun 30 '16 at 09:14
  • Thanks maria i have tried regex pattern. and edited my question. – NEO Jun 30 '16 at 09:18

1 Answers1

2

Description

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

Regular expression visualization

** To see the image better, simply right click the image and select view in new window

This regular expression will do the following:

  • Look ahead and capture the value in COL4 into capture group 1
  • Match the leading characters in COL1 upto the last -01
  • Replaces the value in COL1 with the leading characters followed by the value from COL4

Example

Live Demo

Sample text

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

After Replacement

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

Explanation

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
----------------------------------------------------------------------
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
  • 1
    We tried this expression in Java it solves my purpose :output_row.col1=input_row.co1.substring(0,input_row.col1.lastIndexOf("-")+1)+input_row.col4; Thanks for supporting and Providing me nice solution. Thanks. – NEO Jun 30 '16 at 16:45
  • Sure thing! I'm glad to help. Would you mind marking the answer as excepted if you're satisfied? – Ro Yo Mi Jun 30 '16 at 22:01