s_pattern: Pattern Specifications for String Related Operators

s_patternR Documentation

Pattern Specifications for String Related Operators

Description

The %s-%, %s/%, %ss% operators, as well as the string search operators (str_search), perform pattern matching for some purpose, where the pattern is given in the second argument (p).
When a character vector or string is given as the second argument (p), this is interpreted as case-sensitive regex patterns from 'stringi'.

Instead of giving a string or character vector of regex patterns, one can also supply a list to specify exactly how the pattern should be interpreted. The list should use the exact same argument convention as 'stringi'.

For example:

  • list(regex = p, case_insensitive = FALSE, ...)

  • list(fixed = p, ...)

  • list(coll = p, ...)

  • list(charclass = p, ...)

All arguments in the list are simply passed to the appropriate functions in 'stringi'.
For example:

x %s/% p

counts how often regular expression specified in character vector p occurs in x, whereas the following,

x %s/% list(fixed = p, case_insensitive = TRUE)

will do the same, except it uses fixed (i.e. literal) expression, and it does not distinguish between upper case and lower case characters.

'tinycodet' adds some convenience functions based on the stri_opts_ - functions in 'stringi':

  • s_regex(p, ...) is equivalent to list(regex = p, ...)

  • s_fixed(p, ...) is equivalent to list(fixed = p, ...)

  • s_coll(p, ...) is equivalent to list(coll = p, ...)

  • s_chrcls(p, ...) is equivalent to list(charclass = p, ... )

With the ellipsis (...) being passed to the appropriate 'stringi'-functions when it matches their arguments.

'stringi' infix operators start with "%s", though they all have an alias starting with "%stri". In analogy to that, the above functions start with "s_" rather than "stri_", as they are all meant for operators only.

Usage

s_regex(
  p,
  case_insensitive,
  comments,
  dotall,
  multiline,
  time_limit,
  stack_limit,
  ...
)

s_fixed(p, case_insensitive, overlap, ...)

s_coll(
  p,
  locale,
  strength,
  alternate_shifted,
  french,
  uppercase_first,
  case_level,
  numeric,
  normalization,
  ...
)

s_chrcls(p, ...)

Arguments

p

a character vector giving the pattern to search for.
[REGEX]
[FIXED]
[COLL]
[CHARCLASS]

case_insensitive

see stri_opts_regex and stri_opts_fixed.

comments, dotall, multiline

see stri_opts_regex.

time_limit, stack_limit

see stri_opts_regex.

...

additional arguments not part of the stri_opts - functions to be passed here.
For example: the at argument for the str_search operators.

overlap

see stri_opts_fixed.

locale, strength, alternate_shifted

see stri_opts_collator.

french, normalization, numeric

see stri_opts_collator.

uppercase_first, case_level

see stri_opts_collator.

Value

A list with arguments to be passed to the appropriate operators.

See Also

tinycodet_strings

Examples



x <- c(paste0(letters[1:13], collapse = ""),
       paste0(letters[14:26], collapse = ""))
print(x)
p <- rep("a|e|i|o|u", 2) # same as p <- list(regex = rep("a|e|i|o|u", 2))
x %s/% p # count how often vowels appear in each string of vector x.

x <- c(paste0(letters[1:13], collapse = ""),
       paste0(letters[14:26], collapse = ""))
print(x)
x %s/% list(regex = rep("A|E|I|O|U", 2), case_insensitive = TRUE)
x %s/% s_regex(rep("A|E|I|O|U", 2), case_insensitive = TRUE)


x <- c(paste0(letters[1:13], collapse = ""),
       paste0(letters[14:26], collapse = ""))
print(x)
p <- list(fixed = c("A", "A"), case_insensitive = TRUE)
x %s{}% p
x %s!{}% p
p <- s_fixed(c("A", "A"), case_insensitive = TRUE)
x %s{}% p
x %s!{}% p

x <- c(paste0(letters[1:13], collapse = ""),
      paste0(letters[14:26], collapse = ""), NA)
p <- s_fixed("abc", at = "start")
x %s{}% p
stringi::stri_startswith(x, fixed = "abc") # same as above

p <- s_fixed("xyz", at = "end")
x %s{}% p
stringi::stri_endswith(x, fixed = "xyz") # same as above


tinycodet documentation built on Sept. 12, 2024, 7:03 a.m.