Gsub {lazy.symbolic}R Documentation

Use Friendly Gsub (deprecated as of 2023.05.13)

Description

Use Friendly Gsub (deprecated as of 2023.05.13)

Usage

Gsub(
  expr,
  rule = NULL,
  ...,
  rep = 1,
  value = 0,
  out_pattern = 0,
  split = "?",
  fixed = 0,
  debug = 0
)

Arguments

expr

expression to be modified.

rule

replacement rule to be applied in terms of pattern names.

...

patterns of the form: name=pattern w/o enclosing parens. All of them will be concatenated in the order of appearance and will be used as the pattern to match.

rep

# of times that the pattern and the rule apply.

value

= 1 to print the first matches for each of ....

out_pattern

= 1 to output pattern and replacement rule to be used in gsub.

fixed

fixed parameter to gsub.

debug

= 1 to print useful info

sep

A character which separates each element of the rule.

Details

To be replaced by lazy.toos::Grep.

When value=1 this function interprets ... and print the first match to each element of ....

When rule is given, this function interprets ... and rule and generates the pattern and the replacement for gsub function.

You can use this function in your program code to perform the replacement, or, you can use this to generate the pattern and the replacement for gsub function to be used later with out_pattern=1 option.

The names in ... can be anything as long as they are valid variable names except for rule and those which starts with a period.

rule can contain any string separated by sep. If the string matches one of name elements, the string will be replaced by the pattern assocated with that name.#'

In the original gsub function, the values in ... will be denoted as 1, 2, ... and these numbers must be used in the rule parameter as \\1, \\2, ... .

The following pattern names cannot be used
expr, rule, out_patten, sep, debug

Value

The result of gsub, or when out_pattern=1, the pattern and the rule to be used in gsub in a character vector of length 2.

Examples

# Blow example generates the following gsub patterns:
# gsub(
# "(^|[-+\*`~])([[:alpha:].][[:alnum:]_]*)(\*)([0-9]*\.{0,1}[0-9]*)
# ($|([-+*/`~].+))",   "\1\4\3\2\5",   "a123+a*17.01-b*34+9" )
#

# varname*num -> num*varname
top <- "^|[-+\\*`~]"; varname <- "[[:alpha:].][[:alnum:]_]*"; times <- "\\*"
num <- "[0-9]*\\.{0,1}[0-9]*"; tail <- "$|([-+*/`~].+)"
expr <- "a123+a*17.01-b*34+9"
Gsub( expr, value=1
          , top=top, varname=varname, times=times, num=num, tail=tail )
( res <- Gsub( expr, rule="top?num?times?varname?tail"
          , top=top, varname=varname, times=times, num=num, tail=tail ) )
Gsub( res, rule="top?num?times?varname?tail"
          , top=top, varname=varname, times=times, num=num, tail=tail )
Gsub( expr, rule="top?(?num?)?times?varname?tail", rep=2
          , top=top, varname=varname, times=times, num=num, tail=tail )

# transposition and trace of a scalar
expr <- "+t(09)-tr(123)"
Gsub( expr, rule="top?sign?digits?tail", debug=1
              , top="^|[-+*`~]"
              , tparen="t\\(|tr\\("
              , sign="[-+]*"
              , digits="[0-9]+"
              , paren="\\)"
              , tail="$|[-+*/`~]", rep=2 )


# Cancel x/x etc with back reference
top <- "^|[-/+*=`~]"; varnam <- "[[:alpha:].]+[[:alnum:]_]*|[1-9]+[0-9]*"
div <- "/\\2"; tail <- "($|[-/+*=`~])"
Gsub( "17*x12e/x12e*100+a/a+2/2", rule="top?1?tail"
    , top=top, varnam=varnam, div=div, tail=tail )
Gsub( "17*x12e/x12e*100+a/a+2/2", rule="top?1?tail"
    , top=top, varnam=varnam, div=div, tail=tail, rep=2 )


[Package lazy.symbolic version 0.1.4 ]