strex (version 0.1.3)

str_nth_number_before_mth: Find the \(n\)th number before the \(m\)th occurrence of a pattern.

Description

Given a string, a pattern and natural numbers n and m, find the nth number that comes before the mth occurrence of the pattern.

Usage

str_nth_number_before_mth(string, pattern, n, m, decimals = FALSE,
  leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

str_nth_number_before_first(string, pattern, n, decimals = FALSE, leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

str_nth_number_before_last(string, pattern, n, decimals = FALSE, leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

str_first_number_before_mth(string, pattern, m, decimals = FALSE, leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

str_last_number_before_mth(string, pattern, m, decimals = FALSE, leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

str_first_number_before_first(string, pattern, decimals = FALSE, leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

str_first_number_before_last(string, pattern, decimals = FALSE, leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

str_last_number_before_first(string, pattern, decimals = FALSE, leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

str_last_number_before_last(string, pattern, decimals = FALSE, leading_decimals = FALSE, negs = FALSE, leave_as_string = FALSE)

Arguments

string

A character vector.

pattern

A character vector. Pattern(s) specified like the pattern(s) in the stringr package (e.g. look at stringr::str_locate()). If this has length >1 its length must be the same as that of string.

n, m

Natural numbers.

decimals

Do you want to include the possibility of decimal numbers (TRUE) or not (FALSE, the default).

leading_decimals

Do you want to allow a leading decimal point to be the start of a number?

negs

Do you want to allow negative numbers? Note that double negatives are not handled here (see the examples).

leave_as_string

Do you want to return the number as a string (TRUE) or as numeric (FALSE, the default)?

Value

A numeric vector.

Examples

Run this code
# NOT RUN {
string <- c("abc1abc2abc3abc4def5abc6abc7abc8abc9",
            "abc1def2ghi3abc4def5ghi6abc7def8ghi9")
str_nth_number_before_mth(string, "def", 1, 1)
str_nth_number_before_mth(string, "abc", 2, 3)
str_nth_number_before_first(string, "def", 2)
str_nth_number_before_last(string, "def", -1)
str_first_number_before_mth(string, "abc", 2)
str_last_number_before_mth(string, "def", 1)
str_first_number_before_first(string, "def")
str_first_number_before_last(string, "def")
str_last_number_before_first(string, "def")
str_last_number_before_last(string, "def")
# }

Run the code above in your browser using DataCamp Workspace