Scripting - How to set regular expressions for Format Locator at runtime
Issue
How to change or set regular expressions for Format Locator at runtime?
Sometimes it is not known which regular expressions should be used in a Format Locator until part-way through running locators - for example, if the regular expression for an invoice should be retrieved from a database.
Solution
Update the regular expression in the Format Locator just before it runs, and reset it afterwards to its previous value.
To do this, first add a reference to the Format Locator in script, as per the following screenshot. Note that the Format Locator is called the "Kofax Cascade Format Locator" in the References dialog:
You can use the following code to change the first regular expression or add another regex to your Format Locator via API:
Option Explicit ' Class script: NewClass1 Private Sub Document_BeforeLocate(ByVal pXDoc As CASCADELib.CscXDocument, ByVal LocatorName As String) If LocatorName="FL_InvoiceNumber" Then Dim oFL_InvoiceNumber As CscRegExpLib.CscRegExpLocator Set oFL_InvoiceNumber = Project.ClassByName("NewClass1").Locators.ItemByName("FL_InvoiceNumber").LocatorMethod 'oFL_InvoiceNumber.RegularExpressions(0).RegularExpression = "INV\d{6}" ' changing 1st existing expression ' remove all existing expressions While oFL_InvoiceNumber.RegularExpressions.Count>0 oFL_InvoiceNumber.RegularExpressions.Remove(0) Wend Dim i As Integer i=oFL_InvoiceNumber.RegularExpressions.Count oFL_InvoiceNumber.RegularExpressions.Add("\d{7}") ' adding new expression oFL_InvoiceNumber.RegularExpressions(i).IgnoreCase=True oFL_InvoiceNumber.RegularExpressions(i).IgnoreBlanks=False oFL_InvoiceNumber.RegularExpressions(i).WholeWord=True ' optional way to enable or disable the expression: oFL_InvoiceNumber.RegularExpressions(i).Enabled=true ' optional example: fill locator with values from filename Dim regex As String Dim FileNum As Integer FileNum=FreeFile Open dict.TextFilename For Input As FileNum While Not EOF(FileNum) Line Input #FileNum, regex i=oFL_InvoiceNumber.RegularExpressions.Count oFL_InvoiceNumber.RegularExpressions.Add(regex) oFL_InvoiceNumber.RegularExpressions(i).IgnoreCase=True oFL_InvoiceNumber.RegularExpressions(i).IgnoreBlanks=False oFL_InvoiceNumber.RegularExpressions(i).WholeWord=True 'Debug.Print cstr(i)+":"+regex Wend Close FileNum End If End Sub
If you execute it in Project Builder and check the format locator afterwards then you should see the modification. If you save the project then it will be saved with these changes.
As the format locator does not allow regular expressions in a dictionary you can use this way, too.
A sample KTM project is attached here.
Level of Complexity
Moderate
Applies to
Product | Version | Build | Environment | Hardware |
---|---|---|---|---|
Kofax Transformation Module | 6.3, 6.4 |