> From: bill lam <[email protected]>
>
> I found it very hard to write a regex to match j numbers. A simple
> regex to match positive decimal integer is relatively easy,
>
> d\+
>
> but from page constants in J dictionary,
>
> The form of a numeric constant defined and illustrated in Part I is
> elaborated
> by the use of further letters, as in 2r3 for
> two-thirds, 2p1 for two π, and 2e3p1 for 2000 π. The complete scheme of
> numeric
> constants obeys the following hierarchy:
>
> . The decimal point is obeyed first
> _ The negative sign is obeyed next
> e Exponential (scientific) notation
> ad ar j Complex (magnitude and angle) in degrees or radians; Complex
> number
> p x Numbers based on pi (o.1) and on Euler’s number (the exponential
> ^1)
> b Base value (using a to z for 10 to 35)
>
> Moreover, digits with a trailing x denote an extended precision integer, and
> digits followed by an r followed by further
> digits denote a rational number. See Section II G.
>
> Does anyone know how to write the regex for it?
Maybe I am missing something, but it seems fairly straightforward.
load'regex'
T=: '12 123.4 _12.4 12.3e5 12.3j45.65 12.3p45 16b123 1.5p2j3b100j200'
r0=: '_?[0-9]+(\.[0-9]+)?(e_?[0-9]+)?'
r1=: r0,'((ad|ar|j)',r0,')?'
r2=: r1,'([px]',r1,')?'
r3=: r2,'(b',r2,')?'
r0 rxall T
+--+-----+-----+------+----+-----+----+--+--+---+---+-+-+---+---+
|12|123.4|_12.4|12.3e5|12.3|45.65|12.3|45|16|123|1.5|2|3|100|200|
+--+-----+-----+------+----+-----+----+--+--+---+---+-+-+---+---+
r1 rxall T
+--+-----+-----+------+----------+----+--+--+---+---+---+-------+
|12|123.4|_12.4|12.3e5|12.3j45.65|12.3|45|16|123|1.5|2j3|100j200|
+--+-----+-----+------+----------+----+--+--+---+---+---+-------+
r2 rxall T
+--+-----+-----+------+----------+-------+--+---+-------+-------+
|12|123.4|_12.4|12.3e5|12.3j45.65|12.3p45|16|123|1.5p2j3|100j200|
+--+-----+-----+------+----------+-------+--+---+-------+-------+
r3 rxall T
+--+-----+-----+------+----------+-------+------+---------------+
|12|123.4|_12.4|12.3e5|12.3j45.65|12.3p45|16b123|1.5p2j3b100j200|
+--+-----+-----+------+----------+-------+------+---------------+
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm