How do you match multiple words in regex?

The operation is very useful for checking if a piece of text contains a keyword or phrase. It is however limited to only one word or phrase.

You can extend this by using the match javascript function which can use regex functionality.

An example scenario might be that we are looping through a list of results and each result has an array of 'attachments', with the first [0] attachment containing text. We might want to search through the text attached to each result.

We would do this by adding a script step and setting a variable such as 'text' which uses a jsonpath like $.steps.loop-1.value.attachments[0].text to pull in the text from the result.

This can then be used in the script box where we can add a list of keywords separated by |:

1

exports.step = function (input) {

2

const text = input.text;

3

const res = text.match(

4

/(csv|sheets|boolean|webhook|sso|loop|call workflow|callable|script|object|debug|text helper|data storage|list|http client)/gi

5

);

6

return res;

7

};

This will return results such as:

How do you match multiple words in regex?

Or:

How do you match multiple words in regex?

As you can see this script returns a list of all instances of the keywords found.

For the next step in your workflow you could either extract and count these keywords or, more likely, you could use a boolean connector to check that the result is not null:

Regular expressions are a concise and flexible tool for describing patterns in strings. This vignette describes the key features of stringr’s regular expressions, as implemented by stringi. It is not a tutorial, so if you’re unfamiliar regular expressions, I’d recommend starting at https://r4ds.had.co.nz/strings.html. If you want to master the details, I’d recommend reading the classic Mastering Regular Expressions by Jeffrey E. F. Friedl.

Regular expressions are the default pattern engine in stringr. That means when you use a pattern matching function with a bare string, it’s equivalent to wrapping it in a call to

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
4:

You will need to use

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
4 explicitly if you want to override the default options, as you’ll see in examples below.

Basic matches

The simplest patterns match exact strings:

x <- c("apple", "banana", "pear")
str_extract(x, "an")
#> [1] NA   "an" NA

You can perform a case-insensitive match using

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
6:

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE

The next step up in complexity is

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
7, which matches any character except a newline:

You can allow

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
7 to match everything, including
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
9, by setting
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
0:

Escaping

If “

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
7” matches any character, how do you match a literal “
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
7”? You need to use an “escape” to tell the regular expression you want to match it exactly, not use its special behaviour. Like strings, regexps use the backslash,
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
3, to escape special behaviour. So to match an
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
7, you need the regexp
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
5. Unfortunately this creates a problem. We use strings to represent regular expressions, and
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
3 is also used as an escape symbol in strings. So to create the regular expression
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
5 we need the string
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
8.

# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA

If

# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
3 is used as an escape character in regular expressions, how do you match a literal
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
3? Well you need to escape it, creating the regular expression
# Some Laotian numbers
str_detect("១២៣", "\\d")
#> [1] TRUE
1. To create that regular expression, you need to use a string, which also needs to escape
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
3. That means to match a literal
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
3 you need to write
# Some Laotian numbers
str_detect("១២៣", "\\d")
#> [1] TRUE
4 — you need four backslashes to match one!

In this vignette, I use

# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
5 to denote the regular expression, and
# To create the regular expression, we need \\
dot <- "\\."

# But the expression itself only contains one:
writeLines(dot)
#> \.

# And this tells R to look for an explicit .
str_extract(c("abc", "a.c", "bef"), "a\\.c")
#> [1] NA    "a.c" NA
8 to denote the string that represents the regular expression.

An alternative quoting mechanism is

# Some Laotian numbers
str_detect("១២៣", "\\d")
#> [1] TRUE
7: all the characters in
# Some Laotian numbers
str_detect("១២៣", "\\d")
#> [1] TRUE
8 are treated as exact matches. This is useful if you want to exactly match user input as part of a regular expression.

Special characters

Escapes also allow you to specify individual characters that are otherwise hard to type. You can specify individual unicode characters in five ways, either as a variable number of hex digits (four is most common), or by name:

  • # Some Laotian numbers
    str_detect("១២៣", "\\d")
    #> [1] TRUE
    9: 2 hex digits.

  • (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    0: 1-6 hex digits.

  • (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    1: 4 hex digits.

  • (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    2: 8 hex digits.

  • (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    3, e.g. 
    (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    4 matches the basic smiling emoji.

Similarly, you can specify many common control characters:

  • (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    5: bell.

  • (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    6: match a control-X character.

  • (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    7: escape (
    (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    8).

  • (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"
    9: form feed (
    (text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
    #> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
    str_replace_all(text, "\\p{quotation mark}", "'")
    #> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
    0).

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    9: line feed (
    (text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
    #> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
    str_replace_all(text, "\\p{quotation mark}", "'")
    #> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
    2).

  • (text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
    #> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
    str_replace_all(text, "\\p{quotation mark}", "'")
    #> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
    3: carriage return (
    (text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
    #> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
    str_replace_all(text, "\\p{quotation mark}", "'")
    #> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
    4).

  • (text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
    #> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
    str_replace_all(text, "\\p{quotation mark}", "'")
    #> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
    5: horizontal tabulation (
    (text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
    #> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
    str_replace_all(text, "\\p{quotation mark}", "'")
    #> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
    6).

  • (text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
    #> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
    str_replace_all(text, "\\p{quotation mark}", "'")
    #> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
    7 match an octal character. ‘ooo’ is from one to three octal digits, from 000 to 0377. The leading zero is required.

(Many of these are only of historical interest and are only included here for the sake of completeness.)

Matching multiple characters

There are a number of patterns that match more than one character. You’ve already seen

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
7, which matches any character (except a newline). A closely related operator is
(text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
#> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
str_replace_all(text, "\\p{quotation mark}", "'")
#> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
9, which matches a grapheme cluster, a set of individual elements that form a single symbol. For example, one way of representing “á” is as the letter “a” plus an accent:
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
7 will match the component “a”, while
(text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
#> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
str_replace_all(text, "\\p{quotation mark}", "'")
#> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"
9 will match the complete symbol:

There are five other escaped pairs that match narrower classes of characters:

  • str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    2: matches any digit. The complement,
    str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    3, matches any character that is not a decimal digit.

    Technically,

    str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    2 includes any character in the Unicode Category of Nd (“Number, Decimal Digit”), which also includes numeric symbols from other languages:

    # Some Laotian numbers
    str_detect("១២៣", "\\d")
    #> [1] TRUE

  • str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    5: matches any whitespace. This includes tabs, newlines, form feeds, and any character in the Unicode Z Category (which includes a variety of space characters and other separators.). The complement,
    str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    6, matches any non-whitespace character.

    (text <- "Some  \t badly\n\t\tspaced \f text")
    #> [1] "Some  \t badly\n\t\tspaced \f text"
    str_replace_all(text, "\\s+", " ")
    #> [1] "Some badly spaced text"

  • str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    7 matches any character with specific unicode property, like
    str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    8 or
    str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    9. The complement,
    str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    0, matches all characters without the property. A complete list of unicode properties can be found at .

    (text <- c('"Double quotes"', "«Guillemet»", "“Fancy quotes”"))
    #> [1] "\"Double quotes\"" "«Guillemet»"       "“Fancy quotes”"
    str_replace_all(text, "\\p{quotation mark}", "'")
    #> [1] "'Double quotes'" "'Guillemet'"     "'Fancy quotes'"

  • str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    1 matches any “word” character, which includes alphabetic characters, marks and decimal numbers. The complement,
    str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    2, matches any non-word character.

    str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""

    Technically,

    str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    1 also matches connector punctuation,
    str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    4 (zero width connector), and
    str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    5 (zero width joiner), but these are rarely seen in the wild.

  • str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    6 matches word boundaries, the transition between word and non-word characters.
    str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    7 matches the opposite: boundaries that have either both word or non-word characters on either side.

    str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"

You can also create your own character classes using

str_replace_all("The quick brown fox", "\\b", "_")
#> [1] "_The_ _quick_ _brown_ _fox_"
str_replace_all("The quick brown fox", "\\B", "_")
#> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
8:

  • str_replace_all("The quick brown fox", "\\b", "_")
    #> [1] "_The_ _quick_ _brown_ _fox_"
    str_replace_all("The quick brown fox", "\\B", "_")
    #> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
    9: matches a, b, or c.
  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    0: matches every character between a and z (in Unicode code point order).
  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    1: matches anything except a, b, or c.
  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    2: matches
    str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    3 or
    str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    4.

There are a number of pre-built classes that you can use inside

str_replace_all("The quick brown fox", "\\b", "_")
#> [1] "_The_ _quick_ _brown_ _fox_"
str_replace_all("The quick brown fox", "\\B", "_")
#> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
8:

  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    6: punctuation.
  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    7: letters.
  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    8: lowercase letters.
  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    9: upperclass letters.
  • pattern <- "(..)\\1"
    fruit %>% 
      str_subset(pattern)
    #> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
    #> [6] "salal berry"
    
    fruit %>% 
      str_subset(pattern) %>% 
      str_match(pattern)
    #>      [,1]   [,2]
    #> [1,] "anan" "an"
    #> [2,] "coco" "co"
    #> [3,] "cucu" "cu"
    #> [4,] "juju" "ju"
    #> [5,] "papa" "pa"
    #> [6,] "alal" "al"
    0: digits.
  • pattern <- "(..)\\1"
    fruit %>% 
      str_subset(pattern)
    #> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
    #> [6] "salal berry"
    
    fruit %>% 
      str_subset(pattern) %>% 
      str_match(pattern)
    #>      [,1]   [,2]
    #> [1,] "anan" "an"
    #> [2,] "coco" "co"
    #> [3,] "cucu" "cu"
    #> [4,] "juju" "ju"
    #> [5,] "papa" "pa"
    #> [6,] "alal" "al"
    1: hex digits.
  • pattern <- "(..)\\1"
    fruit %>% 
      str_subset(pattern)
    #> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
    #> [6] "salal berry"
    
    fruit %>% 
      str_subset(pattern) %>% 
      str_match(pattern)
    #>      [,1]   [,2]
    #> [1,] "anan" "an"
    #> [2,] "coco" "co"
    #> [3,] "cucu" "cu"
    #> [4,] "juju" "ju"
    #> [5,] "papa" "pa"
    #> [6,] "alal" "al"
    2: letters and numbers.
  • pattern <- "(..)\\1"
    fruit %>% 
      str_subset(pattern)
    #> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
    #> [6] "salal berry"
    
    fruit %>% 
      str_subset(pattern) %>% 
      str_match(pattern)
    #>      [,1]   [,2]
    #> [1,] "anan" "an"
    #> [2,] "coco" "co"
    #> [3,] "cucu" "cu"
    #> [4,] "juju" "ju"
    #> [5,] "papa" "pa"
    #> [6,] "alal" "al"
    3: control characters.
  • pattern <- "(..)\\1"
    fruit %>% 
      str_subset(pattern)
    #> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
    #> [6] "salal berry"
    
    fruit %>% 
      str_subset(pattern) %>% 
      str_match(pattern)
    #>      [,1]   [,2]
    #> [1,] "anan" "an"
    #> [2,] "coco" "co"
    #> [3,] "cucu" "cu"
    #> [4,] "juju" "ju"
    #> [5,] "papa" "pa"
    #> [6,] "alal" "al"
    4: letters, numbers, and punctuation.
  • pattern <- "(..)\\1"
    fruit %>% 
      str_subset(pattern)
    #> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
    #> [6] "salal berry"
    
    fruit %>% 
      str_subset(pattern) %>% 
      str_match(pattern)
    #>      [,1]   [,2]
    #> [1,] "anan" "an"
    #> [2,] "coco" "co"
    #> [3,] "cucu" "cu"
    #> [4,] "juju" "ju"
    #> [5,] "papa" "pa"
    #> [6,] "alal" "al"
    5: letters, numbers, punctuation, and whitespace.
  • pattern <- "(..)\\1"
    fruit %>% 
      str_subset(pattern)
    #> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
    #> [6] "salal berry"
    
    fruit %>% 
      str_subset(pattern) %>% 
      str_match(pattern)
    #>      [,1]   [,2]
    #> [1,] "anan" "an"
    #> [2,] "coco" "co"
    #> [3,] "cucu" "cu"
    #> [4,] "juju" "ju"
    #> [5,] "papa" "pa"
    #> [6,] "alal" "al"
    6: space characters (basically equivalent to
    str_extract_all("Don't eat that!", "\\w+")[[1]]
    #> [1] "Don"  "t"    "eat"  "that"
    str_split("Don't eat that!", "\\W")[[1]]
    #> [1] "Don"  "t"    "eat"  "that" ""
    5).
  • pattern <- "(..)\\1"
    fruit %>% 
      str_subset(pattern)
    #> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
    #> [6] "salal berry"
    
    fruit %>% 
      str_subset(pattern) %>% 
      str_match(pattern)
    #>      [,1]   [,2]
    #> [1,] "anan" "an"
    #> [2,] "coco" "co"
    #> [3,] "cucu" "cu"
    #> [4,] "juju" "ju"
    #> [5,] "papa" "pa"
    #> [6,] "alal" "al"
    8: space and tab.

These all go inside the

str_replace_all("The quick brown fox", "\\b", "_")
#> [1] "_The_ _quick_ _brown_ _fox_"
str_replace_all("The quick brown fox", "\\B", "_")
#> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"
8 for character classes, i.e. 
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
00 matches all digits, A, and X.

You can also using Unicode properties, like

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
01, and various set operations, like
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
02. See
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
03 for details.

Alternation

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
04 is the alternation operator, which will pick between one or more possible matches. For example,
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
05 will match
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
06 or
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
07:

str_detect(c("abc", "def", "ghi"), "abc|def")
#> [1]  TRUE  TRUE FALSE

Note that the precedence for

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
04 is low:
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
05 is equivalent to
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
10 not
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
11.

Grouping

You can use parentheses to override the default precedence rules:

Parenthesis also define “groups” that you can refer to with backreferences, like

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
12,
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
13 etc, and can be extracted with
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
14. For example, the following regular expression finds all fruits that have a repeated pair of letters:

pattern <- "(..)\\1"
fruit %>% 
  str_subset(pattern)
#> [1] "banana"      "coconut"     "cucumber"    "jujube"      "papaya"     
#> [6] "salal berry"

fruit %>% 
  str_subset(pattern) %>% 
  str_match(pattern)
#>      [,1]   [,2]
#> [1,] "anan" "an"
#> [2,] "coco" "co"
#> [3,] "cucu" "cu"
#> [4,] "juju" "ju"
#> [5,] "papa" "pa"
#> [6,] "alal" "al"

You can use

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
15, the non-grouping parentheses, to control precedence but not capture the match in a group. This is slightly more efficient than capturing parentheses.

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
0

This is most useful for more complex cases where you need to capture matches and control precedence independently.

Anchors

By default, regular expressions will match any part of a string. It’s often useful to anchor the regular expression so that it matches from the start or end of the string:

  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    3 matches the start of string.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    17 matches the end of the string.

To match a literal “$” or “^”, you need to escape them,

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
18, and
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
19.

For multiline strings, you can use

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
20. This changes the behaviour of
str_detect(c("abc", "def", "ghi"), "abc|def")
#> [1]  TRUE  TRUE FALSE
3 and
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
17, and introduces three new operators:

  • str_detect(c("abc", "def", "ghi"), "abc|def")
    #> [1]  TRUE  TRUE FALSE
    3 now matches the start of each line.

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    17 now matches the end of each line.

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    25 matches the start of the input.

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    26 matches the end of the input.

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    27 matches the end of the input, but before the final line terminator, if it exists.

Repetition

You can control how many times a pattern matches with the repetition operators:

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    28: 0 or 1.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    29: 1 or more.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    30: 0 or more.

Note that the precedence of these operators is high, so you can write:

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
31 to match either American or British spellings. That means most uses will need parentheses, like
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
32.

You can also specify the number of matches precisely:

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    33: exactly n
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    34: n or more
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    35: between n and m

By default these matches are “greedy”: they will match the longest string possible. You can make them “lazy”, matching the shortest string possible by putting a

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
28 after them:

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    37: 0 or 1, prefer 0.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    38: 1 or more, match as few times as possible.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    39: 0 or more, match as few times as possible.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    40: n or more, match as few times as possible.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    41: between n and m, , match as few times as possible, but at least n.

You can also make the matches possessive by putting a

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
29 after them, which means that if later parts of the match fail, the repetition will not be re-tried with a smaller number of characters. This is an advanced feature used to improve performance in worst-case scenarios (called “catastrophic backtracking”).

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    43: 0 or 1, possessive.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    44: 1 or more, possessive.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    45: 0 or more, possessive.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    46: exactly n, possessive.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    47: n or more, possessive.
  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    48: between n and m, possessive.

A related concept is the atomic-match parenthesis,

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
49. If a later match fails and the engine needs to back-track, an atomic match is kept as is: it succeeds or fails as a whole. Compare the following two regular expressions:

The atomic match fails because it matches A, and then the next character is a C so it fails. The regular match succeeds because it matches A, but then C doesn’t match, so it back-tracks and tries B instead.

Look arounds

These assertions look ahead or behind the current match without “consuming” any characters (i.e. changing the input position).

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    50: positive look-ahead assertion. Matches if
    # Some Laotian numbers
    str_detect("១២៣", "\\d")
    #> [1] TRUE
    8 matches at the current input.

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    52: negative look-ahead assertion. Matches if
    # Some Laotian numbers
    str_detect("១២៣", "\\d")
    #> [1] TRUE
    8 does not match at the current input.

  • bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    54: positive look-behind assertion. Matches if
    # Some Laotian numbers
    str_detect("១២៣", "\\d")
    #> [1] TRUE
    8 matches text preceding the current position, with the last character of the match being the character just before the current position. Length must be bounded
    (i.e. no
    bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    30 or
    bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    29).

  • (?: negative look-behind assertion. Matches if

    # Some Laotian numbers
    str_detect("១២៣", "\\d")
    #> [1] TRUE
    8 does not match text preceding the current position. Length must be bounded
    (i.e. no
    bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    30 or
    bananas <- c("banana", "Banana", "BANANA")
    str_detect(bananas, "banana")
    #> [1]  TRUE FALSE FALSE
    str_detect(bananas, regex("banana", ignore_case = TRUE))
    #> [1] TRUE TRUE TRUE
    29).

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
61

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
62:

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
2

The second is to use

bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
63. This form ignores spaces and newlines, and anything everything after
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
64. To match a literal space, you’ll need to escape it:
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
#> [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
#> [1] TRUE TRUE TRUE
65. This is a useful way of describing complex regular expressions:

How to match multiple words in regex Java?

You have a structure: word characters + word in double quotes and parentheses. In most cases you can use that kind of alternation builder without any additional steps, just make sure there are no special characters, If there are any, you will need to escape the line with Pattern.

What is multiline regex?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

What does ?= * Mean in regex?

Save this question. . means match any character in regular expressions. * means zero or more occurrences of the SINGLE regex preceding it. My alphabet.txt contains a line abcdefghijklmnopqrstuvwxyz.

How do you match a string with regex?

Syntax: How to Match a String to a Regular Expression Is the character string to match. For example, the regular expression '^Ste(v|ph)en$' matches values starting with Ste followed by either ph or v, and ending with en. Note: The output value is numeric.