Grep {lazy.tools}R Documentation

Use Friendly Grep/Gsub

Description

Use Friendly Grep/Gsub

Usage

Grep(
  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.
No positional arguments are allowed.

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

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 rep,value,out_pattern, split, fixed, debug. name=".v" and name=".n" can be used for a valid variable name and a number, resp..
All of the named arguments in ... will be concatenated in the order of appearance and will be used as the pattern to match.

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

# The third example below generates the following grep/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"

Grep( expr, value=1
          , top=top, varname=varname, times=times, number=num, tail=tail )
Grep( expr, value=1
          , top=top, varname=".v", times=times, number=".n", tail=tail )
( temp <- Grep( expr, rule="top?num?times?varname?tail", out_pattern=1
          , top=top, varname=".v", times=times, num=".n", tail=tail ) )
Grep( temp, rule="top?num?times?varname?tail"
          , top=top, varname=".v", times=times, num=".n", tail=tail )
Grep( expr, rule="top?(?num?)?times?varname?tail", rep=2
          , top=top, varname==".v", times=times, num=".n", tail=tail )

# transposition and trace of a scalar
expr <- "+t(09)-tr(123)"
Grep( 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 <- "($|[-/+*=`~])"
Grep( "17*x12e/x12e*100+a/a+2/2", rule="top?1?tail"
    , top=top, varnam=varnam, div=div, tail=tail )
Grep( "17*x12e/x12e*100+a/a+2/2", rule="top?1?tail"
    , top=top, varnam=varnam, div=div, tail=tail, rep=2 )


[Package lazy.tools version 0.1.4 ]