inverseRegex {inverseRegex}R Documentation

Reverse Engineers a Regular Expression Pattern to Represent the Input Object.

Description

Deconstructs the input into collections of letters, digits, punctuation, and spaces that represent a regex pattern consistent with that input.

Usage

inverseRegex(
  x,
  numbersToKeep = c(2, 3, 4, 5, 10),
  combineCases = FALSE,
  combineAlphanumeric = FALSE,
  combinePunctuation = FALSE,
  combineSpace = FALSE,
  sep = "",
  escapePunctuation = FALSE,
  enclose = FALSE
)

Arguments

x

Object to derive a regex pattern for.

numbersToKeep

Set of numbers giving the length for which elements repeated that many times should be counted explicitly (e.g. "[[:digit:]]{5}"). Repeat sequences not included in numbersToKeep will be coded with a "+" (e.g. "[[:digit:]]+"). Defaults to c(2, 3, 4, 5, 10). Set to NULL to have all runs coded as "+" and set to 2:maxCharacters to have the length specified for all repeated values. If one is included then all unique patterns with be counted as "{1}"; if it is not then the "{1}" is left off.

combineCases

Logical indicating whether to treat upper and lower case letters as a single entity. Defaults to FALSE.

combineAlphanumeric

Logical indicating whether to treat alphabetic characters and digits as a single entity. Defaults to FALSE.

combinePunctuation

Logical indicating whether to treat all punctuation characters as a single entity. Defaults to FALSE.

combineSpace

Logical indicating whether to treat all space characters as a single entity. Defaults to FALSE.

sep

Value used to separate the regex patterns. Defaults to an empty string.

escapePunctuation

Logical indicating whether to escape any punctuation characters. Defaults to FALSE. Set to TRUE if you want to use the returned value as an argument to grep.

enclose

Logical indicating whether to surround each returned value with '^' and '$'. Defaults to FALSE.

Details

The fundamental use of inverseRegex applies only to strings. Inputs of a class other than character are treated as follows:

If these conversion methods are not appropriate then you can do the conversion yourself so that the input is dispatched directly to inverseRegex.character.

The regex patterns are identified using the constructs "[:digit:]", "[:upper:]", "[:lower:]", "[:alpha:]", "[:alnum:]", "[:punct:]", and "[:space:]" as described in ?regex. This will allow for non-ASCII characters to be identified, to the extent supported by grep. Any characters not identified by these search patterns will be left as is. Note that for characters from unicameral alphabets the combineCases argument will need to be set to TRUE otherwise they will not be detected by "lower" and "upper".

NA values in the input will remain as NA values in the output.

Value

A set of regex patterns that match the input data. These patterns will either be character vectors or the same class as the input object if it was a matrix, data frame, or tibble.

Author(s)

Jasper Watson

See Also

occurrencesLessThan, regex

Examples

inverseRegex('Hello World!')

table(inverseRegex(c(rep('HELLO', 10), 'HELL0')))

unique(inverseRegex(iris, numbersToKeep = 1:10))

inverseRegex(c(1, NA, 3.45, NaN, Inf, -Inf))


[Package inverseRegex version 0.1.1 Index]