Bhail
Bhail

Reputation: 407

Is there some way to indicate start and finish of the string I want to match

I am trying to match exact text. In the code below I tried using /b2016/b to split on spaces following 2016, but this matches 0,1,2, or 6. In which way do I indicate start and finish of string I want to match in my code below so that strsplit only splits following the two 2016's. Thanks

x <- "January 2016 February 2016     411,236    410,236     gold   54  end 6  only"

strsplit(x, "(?<=[/b2016/b])\\s+", perl = TRUE)
[[1]]
[1] "January 2016"     "February 2016"    "411,236"         
[4] "410,236"          "gold   54  end 6" "only" 

Upvotes: 0

Views: 46

Answers (3)

jvb
jvb

Reputation: 61

You can better use a gsub function for your goal:

gsub("2016", "2 0 1 6",x)

which is replacing one character sequence by another. The result is:

[1] "January 2 0 1 6 February 2 0 1 6     411,236    410,236     gold   54  end 6  only"

Upvotes: 0

twin
twin

Reputation: 1669

Omit the brackets:

strsplit(x, "(?<=2016)\\s+", perl = TRUE)

Upvotes: 2

akrun
akrun

Reputation: 887251

We can use str_extract to extract only the 4 digit numbers

library(stringr)
str_extract_all(x, "\\b[0-9]{4}\\b")[[1]]

Or to be more exact

str_extract_all(x, "(?<=[A-Za-z]{1,8}\\s)\\b[0-9]{4}\\b")[[1]]
#[1] "2016" "2016"

Upvotes: 1

Related Questions