I know that there is a similar topic on the forum, but there is no answer to my question, and I tried in different ways (R regex - extract words beginning with @ symbol). I need to write out all words that do not have a symbol in front of them.
Below is a code that cuts out all words that contain the #sign and the result of this action.
tweeter <- c("#tweeter tweet", "h#is", "tweet #tweeter2", "twet")
str_extract_all(tweeter, "(?<=\\B\\#)[^\\s]+")
Result of it:
[[1]]
[1] "tweeter"
[[2]]
character(0)
[[3]]
[1] "tweeter2"
[[4]]
character(0)
Now the code with which he tries to display all the words without # at the beginning.
regmatches(tweeter, gregexpr("\\B#\\S+", tweeter), invert = T)
I would like to display a list of words that don't start with #just don't know how to write it properly.
clear() removes all elements from vector and reducing it to size 0. erase() is used to remove specific elements from vector.
Printing all elements without for loop by providing element type: All the elements of a vector can be printed using an STL algorithm copy(). All the elements of a vector can be copied to the output stream by providing elements type while calling the copy() algorithm.
In the for loop, size of vector is calculated for the maximum number of iterations of loop and using at(), the elements are printed. for(int i=0; i < a. size(); i++) std::cout << a.at(i) << ' '; In the main() function, the elements of vector are passed to print them.
This gives you words that doesn't begin with #
library(stringr)
tweeter[!str_detect(tweeter, "^#")]
# "h#is" "tweet #tweeter2" "twet"
str_detect(tweeter, "^#")
returns a logical vector based on the pattern
and in this case ^#
. ^
matches the beginning and #
specifies the character to match at the beginning.
Finally, to return values where the condition is TRUE
we use tweeter[]
.
The same thing can be achieved without using !
which negates the logical value by using negate
argument within the str_detect
as follows:
tweeter[str_detect(tweeter, "^#", negate = TRUE)]
# "h#is" "tweet #tweeter2" "twet"
In base R, we can use grep
with invert
and value
parameter as TRUE
.
grep("^#", tweeter, invert = TRUE, value = TRUE)
#[1] "h#is" "tweet #tweeter2" "twet"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With