Zoho Deluge - Some Useful Regular Expressions

What
A more comprehensive post on some other regex (regular expressions) to format values in Zoho.

How?
The following will remove any non-digits:
v_MyString = "<b>Hello World 123</b>";
v_MyFormattedString = v_MyString.replaceAll("[^0-9]","");
// yields 123

The following will strip all HTML/XML tags:
v_MyString = "<b style='color:red'>Hello World 123</b>";
v_MyFormattedString = v_MyString.replaceAll("<(.|\n)*?>","");
// yields Hello World 123

URL safe slug:
v_MyString = "Hello World 123";
v_MyFormattedString = v_MyString.toLowerCase().replaceAll(" ","-");
v_MyFormattedString = v_MyFormattedString.replaceAll("[^a-z0-9-]+","");
// yields hello-world-123

Email safe string:
v_MyString = "somewhere[]@beyondthesea_1.com";
v_MyFormattedString = v_MyString.toLowerCase().trim();
v_MyFormattedString = v_MyFormattedString.replaceAll("[^a-z0-9@\-.]+","");
// yields This email address is being protected from spambots. You need JavaScript enabled to view it.

Replace any duplicates:
v_MyString = "Hello World Hello Joe";
v_MyFormattedString = v_MyString.replaceAll("(\b\w+\b)(?=.*\b\1\b)","");
// yields World Hello Joe

Remove block comments:
v_MyRegEx = "(\/\*([^*]|(\*+[^*\/]))*\*+\/)";
v_MyString = "String to output: /* this is a comment */";
v_MyFormattedString = v_MyString.replaceAll(v_MyRegEx,"");
// yields String to output:

Zoho Deluge Validation Checking by Regular Expression:

So I could do it the long way in Zoho Deluge. We match a pattern and replace everything out that conforms to the criteria so a true response will be an empty string:
v_MyRegEx = "^[A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,}$";
v_MyString = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
v_MyFormattedString = v_MyString.replaceAll(v_MyRegEx,"");
b_Ok = if( v_MyFormattedString == "", true, false);
// yields true

v_MyString = "somewhere[]@beyondthesea_1.com";
v_MyFormattedString = v_MyString.replaceAll(v_MyRegEx,"");
b_Ok = if( v_MyFormattedString == "", true, false);
// yields false

Yay Zoho has "matches". So this is the regex way to do it:
v_MyRegEx = "^[A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,}$";
v_MyString = "This email address is being protected from spambots. You need JavaScript enabled to view it.";
b_Ok = if( v_MyString.matches( v_MyRegEx  ), true, false);
// yields true

Some other validation checks:
// validate UK postcode
v_MyRegEx = "^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$";

Some XML bits:
v_MyRegEx = "<!\[CDATA\[([^\]]*)\]\]>";
v_MyString = "<tag1><![CDATA[my_real_textual_data]]></tag1>";
v_FormattedString = v_MyString.replaceAll(v_MyRegEx,"$1",false);
// yields <tag1>my_real_textual_data</tag1>


Additional
Since writing this article I found a Zoho page with some other pretty useful regular expressions at https://zoholic.blogspot.com/2015/04/some-usefull-regex-in-zoho.html:

Convert a string of characters to a list:
v_MyString = "12345";
l_StringValues = v_MyString.replaceAll("(?)",",",false).removeFirstOccurence(",").removeLastOccurence(",").toList();
// yields list of [1,2,3,4,5]

// NB: "1mySep2mySep3mySep4mySep5".toList("mySep");
// yields list of [1,2,3,4,5]

Replace any multiple spaces with a single space:
v_MyString = "Hello   World!";
v_FormattedString = v_MyString.replaceAll("[ ]+"," ",false); 
// yields Hello World!

Remove consecutive duplicate words:
// replace consecutive duplicates
v_MyString = "Hello Hello Joe";
v_FormattedString = v_MyString.replaceAll(("([A-Za-z]+) \1"),"$1",false);
// yields Hello Joe

// NB: v_MyString = "Hello World Hello Joe"
// yields Hello World Hello Joe

Escape/Backslash some special characters:
v_MyString = "Joe's \"Amazing\" Skill & <His> Grunt";
v_FormattedString = v_MyString.replaceAll(("([&'\"<>])"),"\\$1",false);
// yields Joe\'s \"Amazing\" Skill \& \<His\> Grunt

Replace new lines in returned JSON (invalid in Zoho):
v_MyString = "{"MyKey":"MyValue
"}";

// the long way (and cos the regex for this isn't working)
v_FormattedString = v_MyString.replaceAll("\r","",false);  // carriage returns
v_FormattedString = v_MyString.replaceAll("\n","",false);  // line feeds
v_FormattedString = v_MyString.replaceAll("\f","",false);  // form feeds

// a regex that should work but doesn't
v_FormattedString = v_MyString.replaceAll(("([\n\r])"),"",false);

Split a string by a word:
v_MyString = "PoshDavid Beckham";
v_FormattedString = v_MyString.replaceAll("\b(Posh)([^ ]*)","$1 and $2",false);
// yields Posh and David Beckham

UK/US Decimal Separator and Commas:
v_MyString = 1234.567;
v_FormattedString = (v_MyString.round(2)).toString().replaceAll(("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)"),",");
// yields 1,234.57

European Decimal Separator and Commas:
v_MyString = 1234.567;
v_FormattedString = (v_MyString.round(2)).toString().replaceAll("\.",",").replaceAll(("(?<!,\d)(?<=\d)(?=(?:\d\d\d)+\b)"),".");
// yields 1.234,57

Extract URL from a link:
v_MyString = "<a href=\"https://www.google.com?searchword=Joe\">Link</a>";
v_FormattedString = v_MyString.replaceAll("^.*href\s*=\s*\"([^\"]*)\".*$","$1");
// yields https://www.google.com?searchword=Joe

Get first 3 words:
v_MyString = "I am Joe the Awesomest";
v_FormattedString = v_MyString.replaceAll("^((?:\S+\s+){2}\S+).*","$1",false);
// yields I am Joe

Not a regular expression but something I use to pad months and dates in a date format:
v_MyString = 2;  // February
v_FormattedString = leftpad(toString(v_MyString), 2).replaceAll(" ", "0");
// yields "02"

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.