NB. Regular expressions extended for Perl-like substitution
NB. Version 2. To use with the new rplc by Chris Burke as of Oct-99
NB. to do: \xHH

require'strings regex user/rplc'

NB. =========================================================
NB.*rxmain v return ()-less mat from ()-ful pattern
rxmain=: ,:"1@:({."2)

NB. =========================================================
NB.*rxs v make Perl-like s/PAT/REPL/OPT substitution
NB. use:
NB.   '/PAT/REPL/OPT' rsx str
NB. PAT - the usual POSIX pattern used in J regex
NB. REPL - the POSIX sed-like replacement string
NB.   \1-\9   corresponding parens content
NB.   \0 or & whole match
NB.   \_      whole match in string representation (for 'e')
NB.   \t      TAB
NB.   \n      LF
NB.   \r      CR
NB.   \f      FF
NB.   \other  other
NB. OPT - any of 'ige' for ignore case, global, execute
NB. see: examples

RBEGE=: <;._1' \n LF \r CR \t TAB \f FF'
RBEGX=: '\n';LF;'\r';CR;'\t';TAB;'\f';FF

rxs=: 4 : 0
esc=. {.x.
'pat rpl opt'=. 3{. <;._1 x.
str=. tolower^:('i'e. opt) y.
pat=. tolower^:('i'e. opt) pat
mat=. pat rxmatch`rxmatches@.('g'e. opt) str
if. (0=#mat) +. _1=1{.,mat do. y. return. end.
subs=. ,:^:(2: > #@$) mat rxfrom y.
mat=. rxmain mat
newr=. ''
  if. 'e' e. opt do.
   r=. rpl rplc '\\';esc;RBEGE
   for_i. i.#mat do.
    pairs=. '&';5!:5<'t' [ t=. >(<i,0){subs
    pairs=. pairs,'\_';'('&,@(,&')')@(5!:5) <'t' [ t=. i{subs
    for_j. i.{:$subs do.
        pairs=. pairs, ('\',":j);5!:5<'t' [ t=. >(<i,j){subs
    end.
    pairs=. pairs,'\';'';esc;'\'
    re=. r rplc pairs
    for_j. i.+/'e'E.opt do.
        re=. (,@":@:".) :: ('__'"_) re
    end.
    newr=. newr,<re
   end.
  else.
   r=. rpl rplc '\\';esc;RBEGX
   for_i. i.#mat do.
    pairs=. '&';>(<i,0){subs
    for_j. i.{:$subs do.
        pairs=. pairs, ('\',":j);>(<i,j){subs
    end.
    pairs=. pairs,'\';'';esc;'\'
    newr=. newr,<r rplc pairs
   end.
  end.
newr mat rxmerge y.
)