R/friendlyGregexpr.R

#' Internal RegEx Function Copied from MPlusAutomation
#'
#' @param pattern x
#' @param charvector x
#' @param perl TRUE/FALSE perl expressions
#'
#' @return
#' @export
friendlyGregexpr <- function (pattern, charvector, perl = TRUE) {
  require(plyr)
  regexpMatches <- gregexpr(pattern, charvector, perl = perl)
  convertMatches <- c()
  for (i in 1:length(regexpMatches)) {
    thisLine <- regexpMatches[[i]]
    if (thisLine[1] != -1) {
      convertMatches <- rbind(convertMatches, data.frame(element = i,
                                                         start = thisLine, end = thisLine + attr(thisLine,
                                                                                                 "match.length") - 1))
    }
  }
  if (is.null(convertMatches))
    return(NULL)
  convertMatches <- plyr::adply(convertMatches, 1, function(row) {
    row$tag <- substr(charvector[row$element], row$start,
                      row$end)
    return(as.data.frame(row, stringAsFactors = FALSE))
  })
  convertMatches$tag <- as.character(convertMatches$tag)
  return(convertMatches)
}
enaY15/TabulationAutomation documentation built on March 18, 2020, 8:35 p.m.