### Regular Expression Constraints

The Regular Expression Constraints is a means for setting restrictions on the content of certain nodes in a document. The restrictions are set as regular expression patterns. The syntax of the patterns is the same as the one in the Grammar tool. The nodes on whose content the constraint will be applied are selected by an XPath expression. The selected nodes must only be of type Element (as no other types of nodes can have content). A node satisfies a certain constraint if its content matches the pattern given in the constraint. The act of application of a constraint gives the user the possibility to navigate either through the nodes which satisfy the constraint or through the nodes which do not satisfy it.

The constraint application is similar to a DTD validation of certain nodes of a document, but here we offer a more powerful instrument. On the one hand, the nodes on which the constraints are applied are selected not only by name (as it is in the DTD), but depending on the context in which they appear (XPath determined context). The context can be an absolute or relative document position, based on properties of the selected nodes or nodes relative to them, properties located in other documents, etc. The node selection qualification uses the full expressive power of the XPath engine, implemented in the system. On the other hand, the regular expressions of the Grammar allow writing patterns not only to the level of text nodes, but to the level of tokens within text nodes. Even more, the user can specify a tokenizer which will be used for segmenting the text and a filter to discard the unmeaningful tokens during application. Here, in the patterns the user can use wildcard symbols in token descriptions in the same way they are used in the Grammar tool.

With the help of these constraints the user can cover some of the features of XML Schema usage, by defining patterns for text nodes.

#### Regular Expression Constraints Structure

Each Regular Expression Constraint consists of 2 main parts and 3 additional(optional) parts:

- Constraint name (obligatory) - a unique identifier for the constraint in the system;
- Regular expression (obligatory) - a valid regular expression which represents the constraint over the nodes' content;
- Default XPath expression - it is an XPath expression defining the selection of the nodes to be processed by the constraint. This expression appears as a default text in the appropriate for this specification area;
- Tokenizer - it is used when the constraint tests text nodes' content. If the tokenizer remains unspecified, then the processor takes the tokenizer, which is specifiied in the DTD of the current document;
- Filter - it is used to filter the tokenizer categories when the text nodes' content is tested. In other words, all tokens to be filtered are discarded from the selection before passing it to the constraint engine.

### Edit Regular Expression Constraints (REC)

This section is responsible for the regular expression constraint management. Here the REC can be created, modified, removed, saved to a file and loaded from a file. Here is a picture of the dialog window:

The left side of the window is a table with all REC in the system. The first column contains the names of the contstraints. The second one contains the regular expressions for each of the constraints. Having selected a row in this table, the user can apply a manipulation over a constraint by using the buttons on the right.

Description of the buttons on the right:

- New - creates a new Regular Expression Constraint. Having pressed the "New" button, a new constraint editor window appears on the screen (for more details, see below);
- Edit - the currently selected constraint is opened for editing in a new editor window;
- Remove - removes the currently selected constraint in the table. The removal is preceded by a confirmation message;
- OK - updates the current changes in the constraints and closes the manager window;
- Cancel - closes the manager window without saving the changes (if any) in the constraints;
- Save To File - serializes all the REC into an external file in an XML format. This function can be used for two main purposes: back-ups and interaction with external applications. The description of the output XML file(the DTD) can be found in the file: regConstraint.dtd;
- Load From File - loads the REC from an external file. The external file must be an XML document, valid with respect to the DTD in the file: regConstraint.dtd;

#### Regular Expression Constraint Editor

Here is the interface view of the editor window for the REC:

The last three fields are optional. The tokenizer and filter lists contain all tokenizers and filters defined in the system. The regular expression may consist of: tags, token categories, token values and token value templates (wildcard descriptions).

### Apply Regular Expression Constraints

The actual applying of the Regular Expression Constraints can be performed in two ways:

- by selecting a node from the tree panel and choosing a constraint;
- by selecting a set of nodes with the help of an XPath expression and then applying a certain constraint on each of them;

Here we describe the latter case. The user chooses 'Apply Regular Expression Constraints' from the menu Tools/Constraints/Regular Expression Constraints/Apply Regular Expression Constraints. Then the following dialog window appears:

The first input field Select nodes contains the XPath which is evaluated in order to select nodes for the constraints operation. If the default XPath expression is specified for the constraint, then it appears in this field as a default text.

The second field selects by name a constraint to be applied.

The last two fields are activated when the constraint tokenizer and filter are ignored and new ones have to be defined explicitly.

Having pressed the Apply button, the XPath is evaluated and a set of nodes is selected. Then for each of them the constraint is applied. If the node's content satisfies the constraint, then the node is marked as Valid. Otherwise it is marked as Non Valid. In this way two groups of nodes are formed and each of them can be observed separately. Here is a picture of the navigation panel window:

Here the user can change the group under observation by using the two radio buttons. Pushing Next and Previous buttons the user changes the current selection in the editor. On the top of the window there is some information about the constraint and the nodes which satisfy or do not satisfy it. For the example above, the pattern '$NUMBER+,$SPACE*' concerns the content of text nodes. The items which satisfy the constraint with this pattern are all element nodes whose text content is a sequence of one or more tokens of category NUMBER, followed by zero or more tokens of category SPACE. Thus, strings which match the pattern are: "1234", "256 ", "666 ", etc.