Regex for String Replace

Hello Mates,

I am looking for a solution to replace below Month and Year format of data.

Month
March 2019
Dec / 2018
Feb / 2019
March-2019
March/2019

To data in this below format:-

Month
Mar-2019
Dec-2018
Feb-2019
Mar-2019
Mar-2019

I am extracting these months using regex. How can i replace it from the 1st format to the above format inside a workflow.

Thanks in advance.

1 Like

Hello @harsh.mehta,
If you are using regext then each date you can treat like an object. Then you could use it in If loop or in Flow Decision and do something like:
If variable date is looking like this then change for this text, else (here another loop) if looking like this then change for this, else… etc.

1 Like

Hi @harsh.mehta,

If you have only the months as a string use the below method.

String1.Replace(" / ","-").Replace(" ","-").Replace("/","-")

2 Likes

Thanks for the quick response @Pablito

But the problem in that would be Month’s are 12 and we know that in advance but Year will be dynamic so making an nested IF loop or Flow decision would not be appropriate solution. As per my understanding.

Thanks

1 Like

@Vivek_Arunagiri

Thank You.

As per your solution one of my problem will solve of changing special characters and space to hyphen.

But how can i change Month from March → Mar, January → Jan and in that format.

How can i replace full month name with its substring of first 3 characters.

1 Like

Please see the below solution. It may not be an optimal solution. But still you can consider.

String1.Replace(" / “,”-“).Replace(” “,”-“).Replace(”/“,”-").Replace(“January”,“Jan”).Replace(“February”,“Feb”).Replace(“March”,“Mar”)…etc

1 Like

@Vivek_Arunagiri
I guess this would work as there can only be 12 cases for months.

While adding it into assign function i am getting an error.

Is this the correct way of using multiple Replace into my assign activity.

Regex.Replace(MonthOfMpr(counter)," / “,”-“).Replace(MonthOfMpr(counter),” “,”-“).Replace(MonthOfMpr(counter),”/“,”-")

Type of variable is which it is assigned to is string and MonthOfMpr(counter) returns string value.

1 Like

This is the not correct way of using Replace function. You can’t use Counter in replace method. Please find the below syntax.

String.Replace(“FindText”,“ReplaceText”).Replace(“FindText”,“ReplaceText”)…etc

1 Like

Hi @harsh.mehta,

Please find the attachment.
Main.xaml (8.1 KB)

Thanks,

@Vivek_Arunagiri
Ok so i need to assign it to an variable first and then pass that variable to Replace function.

Thank you so much for your valuable solution and Time.

1 Like

@Gouda_6

Thank You for the solution i will have a look at the workflow and will revert if it works for me or not.

1 Like

You can use the below regex pattern.

(?<=[JFMASOND][a-z]{2}).*(?=\d{4})

Working Sample Code: Regex to replace month data, Visual Basic - rextester

Regular Expression Details: regex101: build, test, and debug regex

(?<=[JFMASOND][a-z]{2}).*(?=\d{4})

Positive Lookbehind (?<=[JFMASOND][a-z]{2})

Assert that the Regex below matches

Match a single character present in the list below [JFMASOND]

JFMASOND matches a single character in the list JFMASOND (case sensitive)

Match a single character present in the list below [a-z]{2}

{2} Quantifier — Matches exactly 2 times

a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)

.* matches any character (except for line terminators)

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

Positive Lookahead (?=\d{4})

Assert that the Regex below matches

\d{4} matches a digit (equal to [0-9])

{4} Quantifier — Matches exactly 4 times
3 Likes

@KannanSuresh
This works perfect.

Thank you for the solution.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.