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?


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)


It says the code expects a “)”


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



FYI, another approach:

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


1 Like

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