Get the matched value in a string using regex in ax 2012 r3

Hi I’m trying to create a regex for validating date in a string. If the date format is matched in that particular string, I need to store that values from the string to the other variable.

static void ExtractDateFromString(Args _args)
{
    str inputString = "Some text 2022-02-27 more text";
    str pattern = "(\d{4}-\d{2}-\d{2})";
    str extractedDate = System.Text.RegularExpressions.Regex::Match(inputString, pattern).toString();

    if (extractedDate)
    {
        info("Extracted Date: " + extractedDate);

        info("Other Variable Value: " + extractedDate);
    }
    else
    {
        warning("No date pattern found in the input string");
    }
}

I’m getting the output like this…
image

So I’m not sure what is going on , and help me to resolve this issue…

Hi Vignesh, please be careful about which forum you use. If your post wasn’t flagged as spam, I would have never noticed this question in Dynamics 365 Customer Engagement forum.

As you see, calling toString() on CLRObject (containing System.Text.RegularExpressions.Match object) isn’t a good approach. You need to use properties or methods of System.Text.RegularExpressions.Match class to get the information you want.

For example, you could do this:

System.Text.RegularExpressions.Match dateMatch = System.Text.RegularExpressions.Regex::Match(inputString, pattern);
if (dateMatch.get_Success())
{
    extractedDate = dateMatch.get_Value();
}