Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Sunday, June 16, 2024

way to remove special characters from a string using X++

static void RemoveAllSpecialChararcters(Args _args)
{
	str sometext = "ABC#DE%F_$#G@1&23";

	str x = System.Text.RegularExpressions.Regex::Replace(sometext, @"[#,_,$,%,@,&]”, """);
	info(x);
}
static void Dev_ReplaceTxt(Args _args)
{
	TextBuffer buffer = new TextBuffer();
	Str message;
	;

	message = " hi hello's how r u's ";
	message += " How r u doing's wujer's * ? ' what ur mot's anbej's";

	buffer.setText(message);
	buffer.replace("[*?']","\\'"); // replace special character with escape sequence
	info(buffer.getText());

}

strRem('String','*');

If you want to delete the special characters from string, you can use "strAlpha" function. This copies only the alphanumeric characters from a string.

Ex : info(strFmt("%1", strAlpha("?a*b!!!!cD123.")));

results in "abcD123".

Monday, April 22, 2024

Validate multiple emails using X++ , Regex

    ///
    /// </summary>
    /// <param name = "_fieldIdToCheck"></param>
    /// <returns></returns>
    public boolean validateField(FieldId _fieldIdToCheck)
    {
        boolean ret;
    
        ret = super(_fieldIdToCheck);

        switch (_fieldIdToCheck)
        {
            case fieldNum(Reporting, Email) :
              
                container email = str2con(this.Email, SysAppUtilities::semiColon);

                for (int i = 1; i <= conLen(email); i++)
                {
                    if (!Reporting::isValidEmail(conPeek(email, i)))
                    {
                        ret = checkFailed(strFmt("%1: %2", "@SYS334523", conPeek(email, i)));
                    }
                }

                break;
        }
    
        return ret;
    }

    /// <summary>
    ///    This method accepts a email and validates this using REGEX
    /// </summary>
    /// <returns>
    ///    true or false based on Regex match
    /// </returns>
    static server boolean isValidEmail(Email _email)
    {
        System.Text.RegularExpressions.Match match;
        System.Boolean netBool;
        boolean xppBool;
        str matchEmailPattern =
       @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
       [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
     + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
       [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([\w-]+\.)+[a-zA-Z]{2,4})$";
 
        new InteropPermission(InteropKind::ClrInterop).assert();
        match = System.Text.RegularExpressions.Regex::Match(_email, matchEmailPattern);
        netBool = match.get_Success();
        xppBool = netBool;
        CodeAccessPermission::revertAssert();

        return xppBool;
    }

Table browser URL in D365FO

Critical Thinking icon icon by Icons8