Removing part of string before a sub-string and its first occurrence

Removing part of string before a sub-string and its first occurrence



I have a text output like:


str <- '=== AAAA === B§BBB === remove === remove1 === remove2 === AAAA === AAAA'



I would like to remove all the substring between the ===remove (included) and its first next ocurrency:


str_2 <- '=== AAAA === B§BBB === AAAA === AAAA'



I tried in gsub():


gsub()


gsub("=== B§BBB*.*=== AAAA","",str))



But it doesn't work.



Any help is appreciated.




2 Answers
2



Answer to the updated question


str <- '=== AAAA === B§BBB === remove === remove1 === remove2 === AAAA === AAAA'
sub("(?:\s*===\s*remove\S*)+","",str)



See the R demo online and an online regex demo.



The pattern matches 1+ consecutive occurrences of


\s*


===


\s*


remove


\S*



Answer to the original question



You may use


sub("=== remove.*?(n\s*?=== AAAA)","\1",str)



Details


=== remove


.*?


(n\s*?=== AAAA)


1


s*?


=== AAAA



An alternative PCRE regex can also be used:


sub("(?m)(?:(?:^|\R)\h*===\h*remove)+","",str, perl=TRUE)



Details


(?m)


^


(?:(?:^|\R)\h*===\h*remove)+


(?:^|\R)


\h*===\h*


===


remove





I am sorry, I don't have a newline in this string. Editing my question @Wiktor
– thequietus
Aug 22 at 13:02





@thequietus I updated the answer.
– Wiktor Stribiżew
Aug 22 at 18:09



You could use the stringi package.


stringi


library(stringi)
stri_replace_all_fixed(str, " === remove", "")

[1] "=== AAAA === B§BBB === AAAA === AAAA"





edited the question, "remove" string may also vary to "remove1" or whatever @milan. sorry about that.
– thequietus
Aug 22 at 13:27






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)