Why is RegEx not picking up on the following segments within a string?

Viewed 124

I'm fairly new to R.

I'm working on a project that involves scraping from R and analyzing the content for sentiment and other variables. I'm using the RedditExtractoR package and the get_reddit() function to retrieve my data.

Here's an example of what a Reddit comment looks like:

Honestly it\031s right up there with Out of the Woods and Cruel Summer for me so maybe that\031s saying more about me than it does about the songs \005\002 meh I\031m still going to bop

So as you can see, instead of punctuation like apostrophes, I'm seeing a backslash followed by 3 numbers. When I split the string using strsplit(comment, ""), the words and spaces are shown as separate characters, but the backslash and numbers show up as a single character as well (e.g.: "m", "e", "h", " ", "I", "\031", "m")

I've tried a couple of things to try to isolate this weird kind of string but nothing so far has worked. My attempts have included:

grepl("[\\\\]+[[:digit:]]+", comment)
grepl("^.*[\\\\]+[0-9]{3,}.*$", comment)
iconv(comment, from = "ASCII", to = "latin1", sub = "", toRaw = FALSE)

... and many more variations, all of which have returned the value FALSE. Additionally, when I split the string and save the "\031" as a variable, it returns class "character", and returns FALSE to any variation of grepl when the input is anything BUT the full "\031".

What can I try? I'm lost as to why regex wouldn't be able to pick out the backslash and digits.

1 Answers

The \031, defined as a "\031" string literal in an R string, is a control character, called END OF MEDIUM. \005 is an ENQUIRY char, and \002 is START OF TEXT. They all belong to Cc - Other, control Unicode category class.

To check for these chars, you may use

grepl("\\p{Cc}", comment, perl=TRUE)

R demo:

comment <- "Honestly it\031s right up there with Out of the Woods and Cruel Summer for me so maybe that\031s saying more about me than it does about the songs \005\002 meh I\031m still going to bop"
regmatches(comment, gregexpr("\\p{Cc}+", comment, perl=TRUE))
# => [1] "\031"     "\031"     "\005\002" "\031"    
grepl("\\p{Cc}", comment, perl=TRUE)
# => [1] TRUE
Related