rex: Friendly Regular Expressions

複雑な正規表現をわかりやくす表現する

> library(rex)
Welcome to rex, the friendly regular expression helper!
Use 'rex_mode()' to toggle code completion for rex shortcuts and functions.

バージョン: 1.1.1


関数名 概略
%or% Or
as.character.regex Regular Expression
as.regex Coerce objects to a 'regex'.
capture Create a capture group
character_class Create character classes
character_class_escape Character class escapes
counts Counts
escape Escape characters for a regex
group Create a grouped expression
lookarounds Lookarounds
not Do not match
re_matches Match function
re_substitutes Substitute regular expressions in a string with another string.
register_shortcuts Register the Rex shortcuts
rex Generate a regular expression.
rex_mode Toggles 'rex' mode.
shortcuts Shortcuts
single_shortcuts Single shortcuts
wildcards Wildcards

%or%

要素のいずれかにマッチする正規表現

> "A" %or% "B"
Error in eval(expr, envir, enclos): could not find function "%or%"
> or("A", "B")
Error in or("A", "B"): operations are possible only for numeric, logical or complex types
> grep("(?:A|B)", c("A", "B", "C"))
[1] 1 2

character_class

文字クラスの作成

Arguments

  • x
  • ...
  • type
  • start
  • end
> character_class()
> one_of() # 引数に与えた文字のいずれかにマッチする正規表現を返す
> any_of() # 引数に与えた文字と部分的にマッチする正規表現を返す
> some_of()
> none_of() # 引数の文字列とマッチしない正規表現
> except_any_of()
> range() # 引数の文字クラスを作成
> exclude_range() # 引数の文字クラスとマッチしない正規表現を作成
> rex("gr", exclude_range("a", "e"), "y")
> 
> grep("gr[^a-e]*y", "gryy")
> grep(rex("gr", exclude_range("a", "e"), "y"), c("gryy", "gray"))

counts

Arguments

  • x
  • n
  • type... greedy, lazy, possessive
  • low
  • high
> n_times("a", 2)
> grep(between("abc", 2, 4, type = "greedy"), c("abcabc", "abcabcabc"))
> grep("(?:\\d){2,4}", c("1", "12", "123", "1234", "12345", "123456"))
> at_least("\\d", n = 4)
> at_most("\\d", n = 4, type = "possessive")

shortcuts

正規表現のショートカット一覧をリストで返す

> shortcuts
$dot
\.

$any
.

$something
.+

$anything
.*

$start
^

$end
$

$boundary
\b

$non_boundary
\B

$alnum
[:alnum:]

$alpha
[:alpha:]

$letter
[:alpha:]

$blank
[:blank:]

$cntrl
[:cntrl:]

$digit
[:digit:]

$number
[:digit:]

$graph
[:graph:]

$lower
[:lower:]

$print
[:print:]

$punct
[:punct:]

$space
[:space:]

$upper
[:upper:]

$xdigit
[:xdigit:]

$newline
\R

$single_quote
'

$double_quote
"

$quote
'"

$alnums
[[:alnum:]]+

$alphas
[[:alpha:]]+

$letters
[[:alpha:]]+

$blanks
[[:blank:]]+

$cntrls
[[:cntrl:]]+

$digits
[[:digit:]]+

$numbers
[[:digit:]]+

$graphs
[[:graph:]]+

$lowers
[[:lower:]]+

$prints
[[:print:]]+

$puncts
[[:punct:]]+

$spaces
[[:space:]]+

$uppers
[[:upper:]]+

$xdigits
[[:xdigit:]]+

$newlines
\R+

$single_quotes
[']+

$double_quotes
["]+

$quotes
['"]+

$any_alnums
[[:alnum:]]*

$any_alphas
[[:alpha:]]*

$any_letters
[[:alpha:]]*

$any_blanks
[[:blank:]]*

$any_cntrls
[[:cntrl:]]*

$any_digits
[[:digit:]]*

$any_numbers
[[:digit:]]*

$any_graphs
[[:graph:]]*

$any_lowers
[[:lower:]]*

$any_prints
[[:print:]]*

$any_puncts
[[:punct:]]*

$any_spaces
[[:space:]]*

$any_uppers
[[:upper:]]*

$any_xdigits
[[:xdigit:]]*

$any_newlines
\R*

$any_single_quotes
[']*

$any_double_quotes
["]*

$any_quotes
['"]*

$non_alnum
^[:alnum:]

$non_alpha
^[:alpha:]

$non_letter
^[:alpha:]

$non_blank
^[:blank:]

$non_cntrl
^[:cntrl:]

$non_digit
^[:digit:]

$non_number
^[:digit:]

$non_graph
^[:graph:]

$non_lower
^[:lower:]

$non_print
^[:print:]

$non_punct
^[:punct:]

$non_space
^[:space:]

$non_upper
^[:upper:]

$non_xdigit
^[:xdigit:]

$non_newline
^\R

$non_single_quote
^'

$non_double_quote
^"

$non_quote
^'"

$non_alnums
[^[:alnum:]]+

$non_alphas
[^[:alpha:]]+

$non_letters
[^[:alpha:]]+

$non_blanks
[^[:blank:]]+

$non_cntrls
[^[:cntrl:]]+

$non_digits
[^[:digit:]]+

$non_numbers
[^[:digit:]]+

$non_graphs
[^[:graph:]]+

$non_lowers
[^[:lower:]]+

$non_prints
[^[:print:]]+

$non_puncts
[^[:punct:]]+

$non_spaces
[^[:space:]]+

$non_uppers
[^[:upper:]]+

$non_xdigits
[^[:xdigit:]]+

$non_newlines
^\R+

$non_single_quotes
[^']+

$non_double_quotes
[^"]+

$non_quotes
[^'"]+

$any_non_alnums
[^[:alnum:]]*

$any_non_alphas
[^[:alpha:]]*

$any_non_letters
[^[:alpha:]]*

$any_non_blanks
[^[:blank:]]*

$any_non_cntrls
[^[:cntrl:]]*

$any_non_digits
[^[:digit:]]*

$any_non_numbers
[^[:digit:]]*

$any_non_graphs
[^[:graph:]]*

$any_non_lowers
[^[:lower:]]*

$any_non_prints
[^[:print:]]*

$any_non_puncts
[^[:punct:]]*

$any_non_spaces
[^[:space:]]*

$any_non_uppers
[^[:upper:]]*

$any_non_xdigits
[^[:xdigit:]]*

$any_non_newlines
^\R*

$any_non_single_quotes
[^']*

$any_non_double_quotes
[^"]*

$any_non_quotes
[^'"]*

rex

正規表現パターンを生成する

> (rex.str <- rex("gr", one_of("a", "e"), "y"))
gr[ae]y
> grep(rex.str, c("grey", "gray"))
[1] 1 2

rex_mode

> rex_mode()

wildcards

> zero_or_more()
> one_or_more()
> maybe()