Guides/Regular Expressions/Compiling Patterns

From J Wiki
Jump to navigation Jump to search
Overview | Verbs | Basic Patterns | J Patterns | Compiling Patterns

Patterns can be compiled and are represented by integer handles which can be used in their place. This may be of use when there are several patterns that are used repeatedly, to avoid recompiling them on each call.

rxcomp

 phandle=. rxcomp pattern

Compiles a pattern and returns a handle.

rxfree

 rxfree phandle

Releases all resources for a compiled pattern.

rxhandles

 rxhandles ''

Returns a list of all existing handles.

rxinfo

 rxinfo phandle

Returns information about a handle, as the number of subexpressions and the original pattern.

Examples

   [pat1=. rxcomp '(x+)([[:digit:]]+)'
1
   [pat2=. rxcomp '[[:alpha:]][[:alnum:]_]*'
2

   pat1 rxmatch '10 one xxx1234 match'
 7 7
 7 3
10 4

   (pat2;0) rxmatches '10 one xxx1234 match'
 3 3
 7 7
15 5

   rxhandles''
1 2

   rxinfo pat1
┌─┬──────────────────┐
│3│(x+)([[:digit:]]+)│
└─┴──────────────────┘

   rxfree pat1