2

I am looking for an easy way using regex to change the format of a string. this is what I have

POL-CAD-OPT-1-15-31Oct14
INS-DOT-OPT-5-7-13Jun14
BOL-GUP-OPT-5-5-28Oct14

this is what it should be

POL-CAD-OPT-01-15-31Oct14
INS-DOT-OPT-05-07-13Jun14
BOL-GUP-OPT-05-05-28Oct14

right now I am using old split feature to construct the string using if length = 1 then add 0.

is there anything I can do with RegEx ?

1
  • You could probably use the replace functionality in RegEx...would have to search to show you as I haven't done it for ages. +1 though for proper use of RegExes :) Commented Nov 2, 2014 at 22:36

1 Answer 1

3

Edit per comment below : use this pattern
-(\d)(?=-) and replace with -0$1
Demo

-                        '-'
(                        group and capture to \1:
  \d                       digits (0-9)
)                        end of \1
(?=                      look ahead to see if there is:
  -                        '-'
)                        end of look-ahead
Sign up to request clarification or add additional context in comments.

1 Comment

that works but some string like this one, is having issue.. POL-CAD.OPT-4-6-29Sep14.IP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.