R regex

I have a line as shown below.

testSampe <- "Old:windows\r\nNew:linux\r\n"

I want to erase a line between ":"a "\".

Like this "Old\r\nNew\r\n".

How can I create a regex for this?

I tried to execute the gsub function with regex ":.*\\\\", it does not work.

 gsub(":.*\\\\", "\\\\r", testSampe)
+4
source share
2 answers
> testSampe <- "Old:windows\r\nNew:linux\r\n"
> gsub(":[^\r\n]*", "", testSampe)
[1] "Old\r\nNew\r\n"
+4
source

You have a choice of several different regular expressions that will match. See Answer or use falsetru:

rx <- ":[[:alnum:]]*(?=\\r)"

As a more readable alternative gsub, use str_replace_allin package stringr.

library(stringr)
str_replace_all(testSampe, perl(rx), "")
+2
source

Source: https://habr.com/ru/post/1526127/


All Articles