create_pattern {ARUtools} | R Documentation |
Create a pattern to match date
Description
Helper functions to create regular expression patterns to match different metadata in file paths.
Usage
create_pattern_date(order = "ymd", sep = c("_", "-", ""), yr_digits = 4)
create_pattern_time(sep = c("_", "-", ":", ""), seconds = "yes")
create_pattern_dt_sep(sep = "T", optional = FALSE)
create_pattern_aru_id(
arus = c("BARLT", "S\\d(A|U)", "SM\\d", "SMM", "SMA"),
n_digits = c(4, 8),
sep = c("_", "-", ""),
prefix = "",
suffix = ""
)
create_pattern_site_id(
prefix = c("P", "Q"),
p_digits = 2,
sep = c("_", "-"),
suffix = "",
s_digits = 1
)
test_pattern(test, pattern)
Arguments
order |
Character vector. Expected orders of (y)ear, (m)onth and (d)ate. Default is "ymd" for Year-Month-Date order. Can have more than one possible order. |
sep |
Character vector. Expected separator(s) between the pattern parts. Can be "" for no separator. |
yr_digits |
Numeric vector. Number of digits in Year, either 2 or 4. |
seconds |
Character. Whether seconds are included. Options are "yes", "no", "maybe". |
optional |
Logical. Whether the separator should be optional or not. Allows matching on different date/time patterns. |
arus |
Character vector. Pattern(s) identifying the ARU prefix (usually model specific). |
n_digits |
Numeric vector. Number of digits expected to follow the
|
prefix |
Character vector. Prefix(es) for site ids. |
suffix |
Character vector. Suffix(es) for site ids. |
p_digits |
Numeric vector. Number(s) of digits following the |
s_digits |
Numeric vector. Number(s) of digits following the |
test |
Character vector. Examples of text to test. |
pattern |
Character. Regular expression pattern to test. |
Details
By default create_pattern_aru_id()
matches many common ARU patterns like
BARLT0000
, S4A0000
, SM40000
, SMM0000
, SMA0000
.
test_pattern()
is a helper function to see what a regular expression
pattern will pick out of some example text. Can be used to see if your
pattern grabs what you want. This is just a simple wrapper around
stringr::str_extract()
.
Value
Either a pattern (create_pattern_xxx()
) or the text extracted by a
pattern (test_pattern()
)
Functions
-
create_pattern_date()
: Create a pattern to match a date -
create_pattern_time()
: Create a pattern to match a time -
create_pattern_dt_sep()
: Create a pattern to match a date/time separator -
create_pattern_aru_id()
: Create a pattern to match an ARU id -
create_pattern_site_id()
: Create a pattern to match a site id -
test_pattern()
: Test patterns
Examples
create_pattern_date() # Default matches 2020-01-01 or 2020_01_01 or 20200101
# ("-", "_" or "" as separators)
create_pattern_date(sep = "") # Matches only 20200101 (no separator allowed)
create_pattern_time() # Default matches 23_59_59 (_, -, :, as optional separators)
create_pattern_time(sep = "", seconds = "no") # Matches 2359 (no seconds no separators)
create_pattern_dt_sep() # Default matches 'T' as a required separator
create_pattern_dt_sep(optional = TRUE) # 'T' as an optional separator
create_pattern_dt_sep(c("T", "_", "-")) # 'T', '_', or '-' as separators
create_pattern_aru_id()
create_pattern_aru_id(prefix = "CWS")
create_pattern_aru_id(n_digits = 12)
create_pattern_site_id() # Default matches P00-0
create_pattern_site_id(
prefix = "site", p_digits = 3, sep = "",
suffix = c("a", "b", "c"), s_digits = 0
) # Matches site000a
pat <- create_pattern_aru_id(prefix = "CWS")
test_pattern("CWS_BARLT1012", pat) # No luck
pat <- create_pattern_aru_id(prefix = "CWS_")
test_pattern("CWS_BARLT1012", pat) # Ah ha!
pat <- create_pattern_site_id()
pat <- create_pattern_site_id()
test_pattern("P03", pat) # Nope
test_pattern("P03-1", pat) # Success!
pat <- create_pattern_site_id(prefix = "site", p_digits = 3, sep = "", s_digits = 0)
test_pattern("site111", pat)
pat <- create_pattern_site_id(
prefix = "site", p_digits = 3, sep = "",
suffix = c("a", "b", "c"), s_digits = 0
)
test_pattern(c("site9", "site100a"), pat)