str_extract_before: Extract characters in a string which occur before a specified...

Description Usage Arguments Value Examples

View source: R/str_extract_before.R

Description

Vectorised over 'string' and 'pattern'.

Usage

1
str_extract_before(string, pattern, which = "first", num_char = NULL)

Arguments

string

The string from which to extract.

pattern

Pattern to look for.

The default interpretation is a regular expression, as described in stringi::stringi-search-regex. Control options with regex().

Match a fixed string (i.e. by comparing only bytes), using fixed(). This is fast, but approximate. Generally, for matching human text, you'll want coll() which respects character matching rules for the specified locale.

Match character, word, line and sentence boundaries with boundary(). An empty pattern, "", is equivalent to boundary("character").

which

If there are multiple patterns, should the function bring back the "first" or "last" occurrence?

num_char

The number of characters to return from before the pattern. Leave NULL to return everything before the pattern.

Value

The part of the string before the pattern.

Examples

1
2
3
4
test_string <- 'url.com/string_before_pattern_after-another_string_with_pattern2/'
str_extract_before(string = test_string, pattern = '_pattern_')
str_extract_before(string = test_string, pattern = '_pattern_', num_char = 6)
str_extract_before(string = test_string, pattern = '_', which = "last")

johncassil/stringr.plus documentation built on Oct. 9, 2021, 7:56 p.m.