Using Regular Expressions for importing CSV files
Regular Expressions, or regex, are used to identify and manipulate patterns in text. In the ReflexBlue Import/Export Service, regex is used to define a File Mask, allowing you to select only the desired files in a directory for import. By using regex groups, you can isolate specific parts of a filename for processing.
Using regex groups in a File Mask
Groups are an essential concept in regex. By placing parts of a regex pattern in parentheses (), you can define sub-patterns that can be captured and used separately. When used in a File Mask regex, groups allow you to isolate specific parts of a filename as explained in the examples provided below. For more information about using groups in regex patterns, see Regular Expressions Reference.
Example 1
Regex pattern: SalesOrder\(.*\)\.csv
This regex matches any filename that starts with SalesOrder, followed by any characters within parentheses, and ends with .csv. For example, SalesOrder(123).csv or SalesOrder(ABC).csv.
Here, the entire filename is considered as one group. No specific group is defined, so the regex selects the full match.
This can be used effectively when you’re only importing data for a single entity such as sales orders or articles.
Example 2
Regex pattern: SalesOrder\((.*)\)\.csv
This regex is similar to the first example, but with an important difference: the (.*) in the regex defines a group. The part of the filename within the parentheses is now captured as a separate group.
For example, if the filename is SalesOrder(123).csv then the first captured group will be 123.
This approach is ideal when you need to import data for multiple entities simultaneously. For instance, if you’re importing a CSV file with sales orders and another CSV file with corresponding sales order lines, grouping becomes essential. In this scenario, the ReflexBlue Import/Export service uses the defined group to match related files. For example, if the group defined for sales order is 123, the ReflexBlue Import/Export Service will look for the CSV file with sales order lines that also has the group 123 in its name.
When no regex groups are defined in a File Mask
If no group is defined in the regex (i.e., no ()), then the entire filename is considered as a single entity and matched as a whole. This means that the entire name of the file is selected.
For example, in the regex pattern SalesOrder.*\.csv, the entire filename is matched without any division into groups.