Parse regular expression with custom flags and obtain the final pattern to be parsed as well as the vector of flags

parse_regex_pattern(
  pattern,
  authorized_flags,
  parse_flags = TRUE,
  parse_logical = TRUE,
  envir = parent.frame()
)

Arguments

pattern

Character scalar, the regular expression pattern to parse.

authorized_flags

Character vector representing the flags to be parsed. Use the empty string if no flags are allowed.

parse_flags

Logical scalar, default is TRUE. Whether to parse the optional regex flags.

parse_logical

Logical scalar, default is TRUE. Whether to parse logical regex operations, similarly to string_get().

envir

An environment, default is parent.frame(). Only used if the flag magic is present, it is used to find the variables to be interpolated.

Value

This function always returns a list of 4 elements:

  • flags: the character vector of flags. If no flags were found, this is the empty string.

  • patterns: the vector of regex patterns.

  • is_or: logical vector of the same length as patterns. Indicates for each pattern if it should be attached to the previous patterns with a logical OR (FALSE means a logical AND).

  • is_not: logical vector of the same length as patterns. Indicates for each pattern if it should be negated.

Details

This is an internal tool that is exposed in order to facilitate checking what's going on.

There is no error handling.

See also

String operations: string_is(), string_get(), string_clean(), string_split2df(). Chain basic operations with string_ops(). Clean character vectors efficiently with string_clean().

Use string_vec() to create simple string vectors.

String interpolation combined with operation chaining: string_magic(). You can change string_magic default values with string_magic_alias() and add custom operations with string_magic_register_fun().

Display messages while benefiting from string_magic interpolation with cat_magic() and message_magic().

Other tools with aliases: cat_magic_alias(), string_magic(), string_magic_alias(), string_ops_alias(), string_vec_alias()

Author

Laurent Berge

Examples


parse_regex_pattern("f/hello", c("fixed", "ignore"))
#> $flags
#> [1] "fixed"
#> 
#> $patterns
#> [1] "hello"
#> 
#> $is_or
#> [1] FALSE
#> 
#> $is_not
#> [1] FALSE
#> 

x = "john"
parse_regex_pattern("fm/{x} | Doe", c("fixed", "ignore", "magic"))
#> $flags
#> [1] "fixed" "magic"
#> 
#> $patterns
#> [1] "john" "Doe" 
#> 
#> $is_or
#> [1] FALSE  TRUE
#> 
#> $is_not
#> [1] FALSE FALSE
#>