regex last word in string r

Sure! Here is an example of how you can use regular expressions in R to extract the last word from a string:

string <- "This is a sample string"
last_word <- sub(".*\\b(\\w+)$", "\\1", string)

Let's break down the code step by step:

  1. We start by defining a string that we want to extract the last word from. In this example, the string is "This is a sample string".

  2. The sub() function is used to perform a substitution operation on the string. It takes three arguments: the pattern to match, the replacement string, and the string to be modified.

  3. The pattern we use is ".*\\b(\\w+)$". Let's break it down further:

  4. .* matches any character (except a newline) zero or more times. This allows us to match any number of characters before the last word.
  5. \\b is a word boundary anchor that matches the position between a word character and a non-word character. It ensures that we match the last word in the string.
  6. (\\w+) is a capturing group that matches one or more word characters. This allows us to capture the last word.
  7. $ is an anchor that matches the end of the string. It ensures that we match the last word at the end of the string.

  8. The replacement string we use is "\\1". The \\1 refers to the first capturing group, which is the last word in our case. By specifying \\1 as the replacement, we extract the last word from the string.

  9. The result of the sub() function is assigned to the variable last_word. This variable now contains the last word from the original string.

That's it! After running this code, the variable last_word will contain the value "string".