Replacing text with gsub R

Armando González Díaz

I want to replace parts of a text Supposedly gsub would be capabale to do it.

This is an example of the kind of text:

text <- "[2017-12-29 18:24:52] Comentario añadido: SOME RANDOM TEXT I NEED ANALYZE 
[2017-12-29 18:24:52] Comentario añadido: OTHER RANDOM TEXT
[2017-12-29 19:24:52] Comentario añadido: BLA BLA BLA
[2017-12-29 20:24:52] Comentario añadido: BLA BLA BLA BLABLA BLA BLABLA BLA BLA
[2017-12-29 21:24:52] Comentario añadido: BLA BLA BLABLA BLA BLABLA BLA BLA
[2017-12-29 22:24:52] Comentario añadido: BLA BLA BLABLA BLA BLA"

this is the desired result:

"   | SOME RANDOM TEXT I NEED ANALYZE 
    | OTHER RANDOM TEXT
    | BLA BLA BLA
    | BLA BLA BLA BLABLA BLA BLABLA BLA BLA
    | BLA BLA BLABLA BLA BLABLA BLA BLA
    | BLA BLA BLABLA BLA BLA"

the idea is replace that info in order to savbe space and perform another analyzsys with less text.

This is what i get with gsub:

gsub("\\[.*\\] Comentario añadido:", " ° ", text)
[1] " °  BLA BLA BLABLA BLA BLA"

How can i achieve to replace al the cases?

thx in advanced

Wiktor Stribiżew

You may make . not match newlines:

gsub("(?n)\\[.*?] Comentario añadido:", " ° ", text)
      ^^^^

I also advise to use a lazy dot, .*?, just in case there are more ] chars on the line later. See the R demo online.

Another solution may be

gsub("\\[[^][]*] Comentario añadido:", " ° ", text)

Here, [^][]* matches zero or more chars other than ] and [.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related