% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/Grep.R
\name{Grep}
\alias{Grep}
\title{Use Friendly Grep/Gsub}
\usage{
Grep(
  expr,
  rule = NULL,
  ...,
  rep = 1,
  value = 0,
  out_pattern = 0,
  split = "?",
  fixed = 0,
  debug = 0
)
}
\arguments{
\item{expr}{expression to be modified.}

\item{rule}{replacement rule to be applied in terms of pattern names.}

\item{...}{patterns of the form: name=pattern w/o enclosing parens. \cr
No positional arguments are allowed.}

\item{rep}{# of times that the pattern and the rule apply.}

\item{value}{= 1 to print the first matches for each of \code{...}.}

\item{out_pattern}{= 1 to output pattern and replacement rule
to be used in \code{gsub}.}

\item{fixed}{fixed parameter to \code{gsub}.}

\item{debug}{= 1 to print useful info}

\item{sep}{A character which separates each element of the rule.}
}
\value{
The result of \code{gsub}, or when out_pattern=1, the pattern and the rule
 to be used in \code{gsub} in a character vector of length 2.
}
\description{
Use Friendly Grep/Gsub
}
\details{
When \code{value=1} this function interprets \code{...} and
 print the first match to each element of \code{...}.

 When \code{rule} is given,
 this function interprets \code{...} and \code{rule} and generates
 the pattern and the replacement for \code{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
 \code{gsub} function to be used later with \code{out_pattern=1} option.

 The names in ... can be anything as long as they are valid variable names
 except for  \code{rep,value,out_pattern, split, fixed, debug}.
 \code{name=".v"} and \code{name=".n"} can be used for a valid variable name
 and a number, resp.. \cr
 All of the named arguments in ...  will be concatenated in the order
 of appearance and will be used as the pattern to match.

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

 In the original \code{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 \cr
 \code{ expr, rule, out_patten, sep, debug}
}
\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 )

}
