Replace, change positions in a string

Hi, I have a period that I save in a string variable (named Period). This period is read from a web page with get text.

The string is in this format dd-mm-yyyy-dd-mm-yyyy (ex 23-09-2023-30-09-2023)

I want to write the string in the format yyyy-mm-dd-yyyy-mm-dd (2023-09-23-2023-09-30)

How can I do this?

@adrianab.98

Please try this

Say the string is stored in str

str=String.Join("-",System.Text.RegularExpressions.Regex.Matches(str,"\d{2}-\d{2}-\d{4}").Select(function(x) Datetime.ParseExact(x.Value,"dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd").ToArray)

Cheers

It says the code expects a β€œ)”

@adrianab.98

Please try this

str=String.Join("-",System.Text.RegularExpressions.Regex.Matches(str,"\d{2}-\d{2}-\d{4}").Select(function(x) Datetime.ParseExact(x.Value,"dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd")).ToArray)

Missed one bracket

Cheers

Hi,

FYI, another approach:

System.Text.RegularExpressions.Regex.Replace(Period,"(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)","$3-$2-$1-$6-$5-$4")

Sample20230209-2L.zip (2.2 KB)

Regards,

1 Like

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