Accusoft.SmartZoneICR5.Net - Updated
Define and Edit Regular Expressions
SmartZone ICR 5 for .Net - User Guide > How To > Define and Edit Regular Expressions

SmartZone ICR allows you to use Regular Expressions to provide the format of expected data contents. If you know the expected format(s), providing that information will make your recognition results more accurate. (See Regular Expressions for information on syntax.)

 

Use SetRegularExpression method to specify the regular expression string. Note that FieldType must be set to RegularExpression for your regular expression to impact the recognition.

An InvalidRegularExpressionException will be thrown if the regular expression is not valid.

C# Example - Set and get regular expression, and use it in recognition
Copy Code
try
{
   …
   //Set FieldType to RegularExpression and specify regular expression string
   mySmartZoneICR.Reader.FieldType = Accusoft.SmartZoneICRSdk.FieldType.RegularExpression;

   mySmartZoneICR.Reader.SetRegularExpression("(\\d{4}){4}");

   //Get the regular expression string previously set
   string myRegex = mySmartZoneICR.Reader.GetRegularExpression();

   //Perform Recognition
   TextBlockResult textBlockResult = mySmartZoneICR.Reader.AnalyzeField(imageToRecognize.ToHbitmap(false));
   …
}

catch (Accusoft.SmartZoneICRSdk.InvalidRegularExpressionException ex)
{
    MessageBox.Show(ex.Message);
}
catch (Accusoft.SmartZoneICRSdk.BitDepthException ex)
{
    MessageBox.Show(ex.Message);
}

To use regular expression to supplement the recognition of a field type, set the SetFieldRegularExpression method. Supported field types for regular expression are Currency, CurrencyPlus, Date, Email, SocialSecurityNumber, Time, UnitedStatesPhoneNumber, URL, and RegularExpression. For a regular expression example, consider there is a format for part numbers that consisted of alpha and digits, you can use [[alpha]]{2}\d{4} for AB1234.

 

Use GetFieldRegularExpression to get the regular expression of the field type.

C# Example - Set and Get regular expression of a certain field type
Copy Code
…
mySmartZoneICR.Reader.SetFieldRegularExpression(Accusoft.SmartZoneICRSdk.FieldType.Date, "\\d{2}\\/\\d{2}\\/\\d{4}");

string dateRegularExpression = mySmartZoneICR.Reader.GetFieldRegularExpression(Accusoft.SmartZoneICRSdk.FieldType.Date);}
…
Setting a regular expression does not mean it will be used in the actual recognition. You will need to set the FieldType properly for the corresponding regular expression to be used.
C# Example - regular expression and recognition
Copy Code
//Set field regular expression for Currency and Date                
mySmartZoneICR.Reader.SetFieldRegularExpression(Accusoft.SmartZoneICRSdk.FieldType.Date, "\\d{2}\\/\\d{2}\\/\\d{4}");
mySmartZoneICR.Reader.SetFieldRegularExpression(Accusoft.SmartZoneICRSdk.FieldType.Currency, "\\$\\d{2}");
          
//Set FieldType to Date, so only the regular expression of the Date field type will be used in this recognition.
mySmartZoneICR.Reader.FieldType = Accusoft.SmartZoneICRSdk.FieldType.Date;

TextBlockResult textBlockResult = mySmartZoneICR.Reader.AnalyzeField(imageToRecognize.ToHbitmap(false));

//Set FieldType to Currency, and the regular expression of the Currency field type will be used in the following recognition process.
mySmartZoneICR.Reader.FieldType = Accusoft.SmartZoneICRSdk.FieldType.Currency;

TextBlockResult textBlockResult = mySmartZoneICR.Reader.AnalyzeField(imageToRecognize.ToHbitmap(false));
Setting the field regular expression for RegularExpression field type is equivalent to setting regular expression directly.
C# Example - The 2 lines below have the same effect
Copy Code
// The 2 lines below have the same effect
…
mySmartZoneICR.Reader.SetRegularExpression("[A-Z]{3}");
mySmartZoneICR.Reader.SetFieldRegularExpression(Accusoft.SmartZoneICRSdk.FieldType.RegularExpression, "[A-Z]{3}");

…

 

Use ClearFieldRegularExpression to clear the regular expression of the field type.

C# Example
Copy Code
//Clear field regular expression for the Time field type              
……        
mySmartZoneICR.Reader.ClearFieldRegularExpression(Accusoft.SmartZoneICRSdk.FieldType.Time);
……

 

Case insensitivity for all the regular expressions is defined by the property RegularExpressionCaseInsensitivity. This property works for all the regular expressions.

C# Example
Copy Code
……
// Set all regular expression to be case insensitive
mySmartZoneICR.Reader.RegularExpressionCaseInsensitivity = true;
……

 

See Also