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:

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

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 TRUE4 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 TRUE6:

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 TRUE7, 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 TRUE7 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 TRUE9, 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" NA0:

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 TRUE7” 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 TRUE7”? 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" NA3, 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 TRUE7, 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" NA5. 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" NA3 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" NA5 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" NA8.

# 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" NA3 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" NA3? Well you need to escape it, creating the regular expression # Some Laotian numbers str_detect("១២៣", "\\d") #> [1] TRUE1. 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" NA3. 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" NA3 you need to write # Some Laotian numbers str_detect("១២៣", "\\d") #> [1] TRUE4 — 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" NA5 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" NA8 to denote the string that represents the regular expression.

An alternative quoting mechanism is # Some Laotian numbers str_detect("១២៣", "\\d") #> [1] TRUE7: all the characters in # Some Laotian numbers str_detect("១២៣", "\\d") #> [1] TRUE8 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] TRUE9: 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 TRUE9: 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 TRUE7, 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 TRUE7 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 FALSE0: matches every character between a and z (in Unicode code point order).
  • str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE1: matches anything except a, b, or c.
  • str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE2: matches str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE3 or str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE4.

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 FALSE6: punctuation.
  • str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE7: letters.
  • str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE8: lowercase letters.
  • str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE9: 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 TRUE00 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 TRUE01, 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 TRUE02. 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 TRUE03 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 TRUE04 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 TRUE05 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 TRUE06 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 TRUE07:

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 TRUE04 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 TRUE05 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 TRUE10 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 TRUE11.

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 TRUE12, bananas <- c("banana", "Banana", "BANANA") str_detect(bananas, "banana") #> [1] TRUE FALSE FALSE str_detect(bananas, regex("banana", ignore_case = TRUE)) #> [1] TRUE TRUE TRUE13 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 TRUE14. 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 TRUE15, 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 TRUE0

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 FALSE3 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 TRUE17 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 TRUE18, 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 TRUE19.

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 TRUE20. This changes the behaviour of str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE3 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 TRUE17, and introduces three new operators:

  • str_detect(c("abc", "def", "ghi"), "abc|def") #> [1] TRUE TRUE FALSE3 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 TRUE17 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 TRUE25 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 TRUE26 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 TRUE27 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 TRUE28: 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 TRUE29: 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 TRUE30: 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 TRUE31 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 TRUE32.

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 TRUE33: 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 TRUE34: 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 TRUE35: 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 TRUE28 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 TRUE37: 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 TRUE38: 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 TRUE39: 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 TRUE40: 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 TRUE41: 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 TRUE29 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 TRUE43: 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 TRUE44: 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 TRUE45: 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 TRUE46: 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 TRUE47: 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 TRUE48: 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 TRUE49. 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 TRUE50: positive look-ahead assertion. Matches if # Some Laotian numbers str_detect("១២៣", "\\d") #> [1] TRUE8 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 TRUE52: negative look-ahead assertion. Matches if # Some Laotian numbers str_detect("១២៣", "\\d") #> [1] TRUE8 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 TRUE54: positive look-behind assertion. Matches if # Some Laotian numbers str_detect("១២៣", "\\d") #> [1] TRUE8 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 TRUE30 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 TRUE29).

  • (?: negative look-behind assertion. Matches if # Some Laotian numbers str_detect("១២៣", "\\d") #> [1] TRUE8 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 TRUE30 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 TRUE29).

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

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

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

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 TRUE63. 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 TRUE64. 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 TRUE65. 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.

Postingan terbaru

LIHAT SEMUA