regexp() function in Lua scripts returns unexpected results
search cancel

regexp() function in Lua scripts returns unexpected results

book

Article ID: 276849

calendar_today

Updated On:

Products

DX Unified Infrastructure Management (Nimsoft / UIM)

Issue/Introduction

The NSA Lua SDK (as well as the NAS scripting functionality) supports a function called "regexp()" which can be used to determine whether a particular string matches a given regular expression.

However we are seeing some strange behaviors and getting unexpected false or true results with certain patterns.

For example:

TestString="ABC123"
result1 = regexp(TestString, "/") -- this should not match, because the TestString does not contain a forward slash.
result2 = regexp(TestString, "?") -- this should not match, because ? is supposed to match only a single character

print(result1)
print(result2)

Output:

True
True

Generally, patterns with asterisk (*) or question mark (?) or a single forward slash (/) are causing unexpected results/false positives.

 

Environment

NAS 23.4.0 or lower
NSA SDK 20.50 or lower

Cause

The regexp() function internally relies on another function in the SDK which is capable of performing both regexp matching and "simple" pattern matching, where asterisk (*) matches anything (wildcard) and question mark (?) matches any single character.

This can cause unexpected results because the regexp() function expects a true regular expression but the underlying SDK function will try to "translate" simple patterns to regex.

Additionally, there is a defect in the case where the matching expression is a single slash which has been resolved in later versions of the SDK.

 

Resolution

For the SDK, upgrade to NSA 20.60 or higher to resolve the issue with forward slash matching.

For NAS, upgrade to UIM 23.4.1  (UIM 23.4 CU1).

Otherwise, this problem can be avoided by ensuring the following:

1. You always feed a proper regular expression to the regexp() function and not a simple pattern match.  For simple patterns, use Lua functions like string.find() or string.gsub() instead.
2. The regular expression should be enclosed in double forward slashes.

 

Example (as given in the introduction):

TestString = "ABC123"
result = regexp(TestString, "/^A.*$/"))

The double-slash enclosure ensures that the regular expression is interpreted as PCRE (Perl Compatible Regular Expression).

Additional Information

You can use https://www.regex101.com for Regular Expression (PCRE) validation.

More information on Lua pattern matching is available in the Lua documentation.