4
I have this variable:
y <- c('stack overflow', 'user number 2', 'nova\nlinha', 'nova \n linha')
And these functions with their results:
library(tidyverse)
With \n:
str_detect(string = y, regex(pattern = '\\n'))
[1] FALSE FALSE TRUE TRUE
With \s:
str_detect(string = y, regex(pattern = '\\s'))
[1] TRUE TRUE TRUE TRUE
In the strings 'nova\nlinha', 'nova \n linha', while in the first there are no spaces but in the second yes, the return of the function is TRUE for both cases.
I tried to use the \t, as stated in this question:
str_detect(string = y, regex(pattern = '[ \\t]'))
[1] TRUE TRUE FALSE TRUE
Worked properly.
Well, then I had some doubts. In the documentation of regex, \t operates differently. He looks for a tab in string. I have two questions facing this:
what is the difference of
tabfor spacing and in which situationstabsare more common than spaces?why did the last function I wrote work? I used it, but I didn’t understand its logic (this one:
str_detect(string = y, regex(pattern = '[ \\t]'))).
NOTE: Use R in Linux and you need to use the double bar (\\) to operate instead of a (\). So, for example, instead of the conventional \s should use the \\s.
I replied about the regex, but as for the use of TAB x space, I think it is too broad a subject that goes beyond the scope of the regex: https://www.google.com/search?q=tab+vs+space
– hkotsubo
Recommended reading on "tabs vs spaces".
– neves