R sub/gsub replacing first occurence of match

ben

In R, I need to extract "Eight" from the following string:

this_str <- " Eight years blah blah 50 blah blah, two years blah blah blah."

Here is my attempt using gsub:

gsub("^.*\\s([^ ]*)\\s(years|months)\\s.*", "\\1", this_str)

But this returns "two", which corresponds to the second occurrence of the pattern indicated in gsub(). In other posts it is said that sub() should return the first match. But when I use sub() it also gives "two".

Julius Vainora

sub does a single replacement, while gsub does multiple ones. Instead the issue is that .* at the beginning is greedy: it goes up to "two" (i.e., includes all but the last match). Instead we want to be lazy (see here) and match as little as possible:

sub("^.*?\\s([^ ]*)\\s(years|months)\\s.*", "\\1", this_str)
# [1] "Eight"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regex: Replacing the first leftmost occurence or the first rightmost occurence

Replacing just the first occurence of a character in a line

Replacing elements in a string besides the first Occurence In Scala

Replacing first occurence except comments using sed

grep match till the first occurence of a character

Match until the first occurence of the last character of a string

Regular expression match all except first occurence

pcregrep: how to match only the first occurence?

Remove first occurence of match in regex golang

AIX - awk display first occurence of each match

How to stop match at first occurence of a match on each line?

R: regex first occurence based on condition

R: Fast string split on first delimiter occurence

R: Find first occurence of value for each day

Regex to match first occurence of a string starting and ending with predefined characters

How to match only first occurence of a string per line with regex

Regex to match every occurence of a character except the first one

R regex - replacing nth match

Issue with replacing string by match in R

Replacing only the first match of a global regex

sed multiple matches replacing first match value

R - regular expression, match after second or third occurence

Identify first occurence in dataframe in R and subsequently label every third row

Select the row conditioning on the first occurence of a fixed value using R

in R, change dataframe column from the first occurence of a second column

R make.unique start at 2 and leave the first occurence

First occurence in table based on values of another table in R

How to count the number of occurence of First Charcter of each string of a column in R

How can I make the match fail if the first occurence of a pattern doesn't match a condition?