Overview
xTuple ERP supports the use of "regular expressions" (also known as "Regex") in fields where pattern matching is called for. This regular expression support gives you tremendous flexibility and control whenever you want to retrieve unique patterns from within various data sets (e.g., class codes, product categories, customer types, etc.).
Let's say, for example, you want to generate a report showing Internet sales during a given period. To access this data, you would need to look at sales made to your Internet customers. Since "Internet Customers" are a subset of customer types, you would create a regular expression to retrieve this subset and send it to the report.
Characters and Meta-characters
Regular expressions are created using different combinations of characters and meta-characters. A character is defined as any alphanumeric character — both upper and lower case — including punctuation marks, white space, and other keyboard symbols. Meta-characters, sometimes referred to as "wildcards," are special characters used to facilitate pattern matching. The most common meta-characters are described in the tables below.
Info: The more you understand the role of meta-characters, the more you will be able to control your pattern matching.
The first thing to understand is that regular expressions match "sub-strings." A sub-string is a subset of a "string"—a string being a sequence of characters arranged in a line. For example, the numbers, "123" are a sub-string of the string 012345. Similarly, the letters EAT are a sub-string of the strings MEAT, EATERY, and THEATER. Numbers and letters can also be combined to form a sub-string. The pattern, INTCUST4, is a sub-string of a particular set of Internet customer strings: INTCUST400, INTCUST401, INTCUST402, and so on. As you can see, sub-strings may appear anywhere in a string—at the beginning, the middle, or the end.
Info: Regular expressions are case-sensitive: this means there is a difference between "a" and "A." Keep this in mind when using regular expressions for pattern matching.
Meta-characters give you even more control over your sub-string definitions. With meta-characters, you can specify the exact location of a sub-string: beginning of a word or line, end of a word or line, etc. You can also specify ranges of data: customers A-Z, items 1-9, etc. This sort of control is especially vital when searching through large quantities of data. As you can imagine, meta-characters will not only save you time, they will also increase your precision. The following tables describe meta-characters in more detail:
|
Single Character Meta-characters |
|
|
. |
Matches any single character. |
|
[...] |
Matches any single character listed between the brackets. |
|
[^...] |
Matches any single character except those listed between the brackets. |
Note: xTuple ERP supports pattern matching with regular expressions in accordance with the "Portable Operating System Interface" (POSIX) standard.
|
Quantifiers |
|
|
? |
Matches the preceding element zero or one time. |
|
* |
Matches the preceding element zero or more times. |
|
+ |
Matches the preceding element one or more times. |
| | | Operates as a choice between alternatives, equivalent to "or". Example: * The expression abc|def would match "abc" or "def." |
|
{num} |
Matches the preceding element num times. |
|
{min,max} |
Matches the preceding element at least "min" times, but not more than "max" times. |
Info: A space between characters is considered a character itself. You should avoid using spaces when writing regular expressions, unless the pattern you are matching expressly calls for them.
|
Anchors |
|
|
^ |
Matches at the start of the line. |
|
$ |
Matches at the end of the line. |
Info: Build regular expressions using trial and error. If your first attempt doesn't yield the desired results, modify the expression and try again.
Hierarchical Structure
To simplify your pattern matching efforts, you should organize your groupings according to a hierarchical structure. Again, the following groupings support pattern matching using regular expressions:
- Class Codes
- Planner Codes
- Item Groups
- Product Categories
- Customer Types
- Customer Groups
Let's consider the customer type grouping to illustrate this point about hierarchies. If your customer types have been arranged hierarchically, the naming convention will exhibit a logical, sequential order. The following list shows an orderly, hierarchical arrangement of customer types:
- CUSTUSA100, 101, 102, ...
- CUSTEUROPE100, 101, 102, ...
- CUSTASIA100, 101, 102, ...
Regular expressions will find and match any pattern, but writing them will be easier if you arrange your groupings hierarchically, as shown in the example above. For more detailed information about pattern matching there are numerous websites available, including this one.