Skip to content
Axes and tools

Axes and tools

Read a property of every token, its scale here, and query it from a pattern; meet the other eleven axes one example each; then rewrite what a pattern finds.

Read an axis

A token has a scale, a nesting depth, a texture and more; each is an axis, read on its own or from a pattern. Magnitude is the order of magnitude of a token’s value:

$ trex magnitude --text 'retries = 3 ; max_bytes = 5000000000'
trex magnitude: 36 bytes, 7 tokens, total energy 112.2, 3 scale-jump(s)
  peak magnitude: 9.70 at '5000000000'

Query it from a pattern

\M{>6} is a token of more than six orders of magnitude:

$ trex scan '\M{>6}' --text 'retries = 3 ; max_bytes = 5000000000'
[26..36] "5000000000"

The other axes

Each of the other axes reads one more property of the input. One example of each follows; the axis pages give every reading, how to choose its thresholds, and how it is built.

Stress

Stress reads how deep in brackets a token is. @nested>=2 holds two brackets deep or more:

$ trex scan '@nested>=2 \W' --text 'f(a, g(b, h(c)))'
[7..8] "b"
[10..11] "h"
[12..13] "c"

Flow

Flow reads the trend of a signal along the input, magnitude by default, and where it turns. The slope averages over the four tokens behind, so the turn after 729 reads at 27:

$ trex flow --reversals --text '3 9 27 81 243 729 243 81 27 9'
trex flow (signal magnitude): 29 bytes, 10 tokens, 1 reversal(s)
  peak momentum: 6 (rising)
  reversals (1):
    @    25  27 9

Shape

Shape reads each token’s silhouette, its kind and a word’s shape, and the period the silhouettes repeat at. Rows of different widths read one silhouette, so the period is the row’s five tokens:

$ trex shape --text 'ann,12,3 bo,1200,1 cy,7,22 dee,450,4'
trex shape: 36 bytes, 20 tokens, 1 template region(s), 2 change-point(s)
  dominant shape-period: 5 tokens  (strength 1.00)

Spectral

Spectral reads the bytes as a signal, their class mix, entropy and period, and where they change. With the token shapes it classifies the regions of an input; the code in this paragraph reads as code, with the cut a few bytes in, where the bytes before it change:

$ trex spectral --classify --text 'The report lists every host that answered within the window. a[i]=(b[j]*c[k]+d[i-1])>>2;if(a[i]>m){m=a[i];k=i;} The totals go back to the collector before the next window opens.'
trex spectral (region classification: shape x spectral): 177 bytes, 3 regions
    [       0..65      ] prose      The report lists every host 
    [      65..117     ] code       =(b[j]*c[k]+d[i-1])>>2;if(a[
    [     117..177     ] prose      otals go back to the collect

Seam

Seam cuts where the bytes before a point stop predicting the bytes after it. With the built-in English letter model it finds the words of a string with no spaces:

$ trex seam --english --text 'thebirdsflyoverthehouses'
trex seam: 24 bytes, order 3, 6 segments (bidirectional branching entropy)
  segments:
    [     0..3     ] "the"
    [     3..8     ] "birds"
    [     8..11    ] "fly"
    [    11..15    ] "over"
    [    15..18    ] "the"
    [    18..24    ] "houses"

Observation

Observation reads the input from behind a point, from ahead of it and around it, and marks where the readings disagree. @ambiguous:token holds at the last value of a table that prose follows, where what came before and what comes after read the point differently:

$ trex scan '@ambiguous:token .' --text 'id,host,bytes
1,alpha,1024
2,beta,2048
The export finished without errors.'
[34..38] "2048"

Echo

Echo reads whether a token’s content returns, how often and how far away. @novel holds at a content’s first occurrence:

$ trex scan '@novel \W' --text 'cat dog cat bird dog cat'
[0..3] "cat"
[4..7] "dog"
[12..16] "bird"

Orbit

Orbit reads a token up to a symmetry, such as case, notation or word shape; the orbit page lists every group. (?orbit:case ...) matches a literal in any case:

$ trex scan '(?orbit:case "cat")' --text 'Cat CAT dog cat'
[0..3] "Cat"
[4..7] "CAT"
[12..15] "cat"

Relation

Relation reads the graph the brackets and names make. Its canonical form writes a name a bracket binds as # and each use of it as ^k, so two texts that differ only in their names read alike:

$ trex relation --canonical --text '[ x ( x ) ]'
... the readings ...
  canonical form (alpha-equivalence):
    [ # ( ^0 ) ]

Gravity

Gravity reads which types of token the input puts near each other more or less often than chance, and how hard a token’s past pushes it away. In a log that puts a number after every = but two, @strain>0b takes the two words in a number’s place:

$ trex scan '@strain>0b .' --text 'job=17 rows=1200 rows=1180 disk=sda rows=1210 rows=3590 disk=sdb rows=1190 job=17'
[32..35] "sda"
[61..64] "sdb"

Context

Context compares a token with what surrounds it: the window before it, its column of a record, or the values its key held before. \N{>+1:k} takes a value more than an order of magnitude above the values bound to the same key:

$ trex scan '"latency":k "=" \N{>+1:k}' --text 'latency = 100 ; latency = 120 ; size = 5000000 ; latency = 90 ; latency = 12000 ;'
[64..79] "latency = 12000"  captures: k="latency"

Rewrite what a pattern finds

A template replaces each match: ${name} renders a register and ${name:acc} a slice of it:

$ trex rewrite '\E:e' '[${e:domain}]' --text 'mail bob@x.com now'
mail [x.com] now

Where next

  • Beyond regex covers the rest of the pattern language.
  • The how-to guides solve one task each: grammars, prefilters, records, rules.
  • The reference is the complete specification.
  • The explanation covers how the engine reads tokens in one pass.