Regular expression is a powerful tool for matching a text pattern, but it is not often seen in AX.
I've seen a developer coded a method today to validate a text string, return true if the text contains digit only (0-9), return false if either one of the character in the text contain non-digit, the method is ~6 lines long, find and loop through each character.
There's an easier way to achieve the same result - use of Regular expression.
The method can be simplified to 1 line.
This change, saved 0.31ms (from the trace captured), although the number looks small, but when this is executed millions time, it does cost a lot.
In the sample file I received, it imports around 20 million lines, given the 0.31ms saving, that reduce the execution time by:
> Time saved in ms x 20m lines = 6,200,000ms
> Time saved in seconds = 6,200 seconds
> Time saved in minutes = 103 minutes
> Time saved in hours = 1.72 hours
Using regular expression ===============================
//Return TRUE if '_text' contain digit only, otherwise return FALSE
boolean validateTextMatch(str _text)
{
;
return !match("[^0-9]", _text);
}
Using strFind() & subStr() in loop =========================
boolean
validateTextLoop(str _text)
{
int counter, textLen = strlen(_text);
Phone validNumbers = '0123456789';
;
for(counter = 1; counter <= textLen; counter ++)
{
if(!strfind(substr(_text,counter,1), validNumbers, 1, 10))
{
return false;
}
}
return true;
}
|
Screenshot showing the trace time of both methods
|