removing google ads codes from text

I have a tibble with a text column, which I scrapped from a website. the text came with two chunks of google ads code. One in the middle and one at the end. I am trying to remove it using str_remove(), but cannot do. one entry from the text is produced below, with google ads code in bold:

As has happened during previous years, when Palestinians mark the mass displacement of their people, there has been terrible violence this year too on the Gaza frontier. Israeli soldiers shot dead 16 people as large crowds gathered at the frontier in Gaza to mark the day when more than 700,000 people fled from towns and villages as the state of Israel was created with the support of the US and its Western allies. Hamas, the Palestinian nationalist group which has ruled Gaza since 2007, had called for marches to commemorate the violent ouster of Palestinians from their homes. At least 65 persons including children and paramedics were injured in the firing by Israeli soldiers. Israel describes Hamas as a fundamentalist, Islamic organisation – a tag the organisation refuses to acknowledge. Almost a year ago, Israeli snipers had killed 60 people when thousands came out to protest the opening of the US embassy in a disputed part of Jerusalem which Palestinians claim as part of their territory. Widening violence has taken place between Israeli forces and Palestinians since then, with more than 200 killed and thousands wounded. A UN inquiry is underway to ascertain if Israeli actions against journalists, activists and children amount to war crimes. .ads_between_content{ height:auto; width:auto; float:right; margin:10px 0 10px 20px; } @media (max-width:600px){ .ads_between_content{ float:none; margin:0px auto; text-align: center; } } googletag.cmd.push(function() { googletag.display('div-gpt-ad-1531458005988-0'); }); These tensions have added to concerns about a fourth war breaking out between Hamas-led Palestine and Israel. There have been three wars fought between Arabs and Israelis since 1948. A US-led plan is said to be on the drawing board to build outlines for a permanent settlement to the dispute over the territory which was created in 1948 by sending five million Palestinian refugees into exile as a new country was carved out by force in the Middle East. There are no details on what this peace plan may entail. This is an issue that has divided the world now for seven decades. The basic acceptance of the fact that Israel was created against the will of the Palestinians is occluded by the fact that the territory of Israel is considered a holy place by Jews, Christians and Muslims. How can that be any excuse for denying the Palestinians their rights? Their land was stolen from them over 70 years ago. They deserve for it to be returned – and most importantly of all for peace to be given back to them within a territory that they can once again call home. googletag.cmd.push(function () { googletag.display('div-gpt-ad-1523347863262-14'); });

this is how I apply the code

tibble %>% mutate (text = str_remove("googletag.cmd.push(function () { googletag.display('div-gpt-ad-1523347863262-14'); });", ""))

If all the google add codes follow the same pattern, this would work

library(tidyverse)
# Sample data
df <- data.frame(stringsAsFactors = FALSE,
                 text = "As has happened during previous years, when Palestinians mark the mass displacement of their people, there has been terrible violence this year too on the Gaza frontier. Israeli soldiers shot dead 16 people as large crowds gathered at the frontier in Gaza to mark the day when more than 700,000 people fled from towns and villages as the state of Israel was created with the support of the US and its Western allies. Hamas, the Palestinian nationalist group which has ruled Gaza since 2007, had called for marches to commemorate the violent ouster of Palestinians from their homes. At least 65 persons including children and paramedics were injured in the firing by Israeli soldiers. Israel describes Hamas as a fundamentalist, Islamic organisation – a tag the organisation refuses to acknowledge. Almost a year ago, Israeli snipers had killed 60 people when thousands came out to protest the opening of the US embassy in a disputed part of Jerusalem which Palestinians claim as part of their territory. Widening violence has taken place between Israeli forces and Palestinians since then, with more than 200 killed and thousands wounded. A UN inquiry is underway to ascertain if Israeli actions against journalists, activists and children amount to war crimes. .ads_between_content{ height:auto; width:auto; float:right; margin:10px 0 10px 20px; } @media (max-width:600px){ .ads_between_content{ float:none; margin:0px auto; text-align: center; } } googletag.cmd.push(function() { googletag.display('div-gpt-ad-1531458005988-0'); }); These tensions have added to concerns about a fourth war breaking out between Hamas-led Palestine and Israel. There have been three wars fought between Arabs and Israelis since 1948. A US-led plan is said to be on the drawing board to build outlines for a permanent settlement to the dispute over the territory which was created in 1948 by sending five million Palestinian refugees into exile as a new country was carved out by force in the Middle East. There are no details on what this peace plan may entail. This is an issue that has divided the world now for seven decades. The basic acceptance of the fact that Israel was created against the will of the Palestinians is occluded by the fact that the territory of Israel is considered a holy place by Jews, Christians and Muslims. How can that be any excuse for denying the Palestinians their rights? Their land was stolen from them over 70 years ago. They deserve for it to be returned – and most importantly of all for peace to be given back to them within a territory that they can once again call home. googletag.cmd.push(function () { googletag.display('div-gpt-ad-1523347863262-14'); });")
df %>% 
    mutate(text = str_remove_all(text, "(\\.ads|googletag).+?\\}\\);\\s?"))
#>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  text
#> 1 As has happened during previous years, when Palestinians mark the mass displacement of their people, there has been terrible violence this year too on the Gaza frontier. Israeli soldiers shot dead 16 people as large crowds gathered at the frontier in Gaza to mark the day when more than 700,000 people fled from towns and villages as the state of Israel was created with the support of the US and its Western allies. Hamas, the Palestinian nationalist group which has ruled Gaza since 2007, had called for marches to commemorate the violent ouster of Palestinians from their homes. At least 65 persons including children and paramedics were injured in the firing by Israeli soldiers. Israel describes Hamas as a fundamentalist, Islamic organisation – a tag the organisation refuses to acknowledge. Almost a year ago, Israeli snipers had killed 60 people when thousands came out to protest the opening of the US embassy in a disputed part of Jerusalem which Palestinians claim as part of their territory. Widening violence has taken place between Israeli forces and Palestinians since then, with more than 200 killed and thousands wounded. A UN inquiry is underway to ascertain if Israeli actions against journalists, activists and children amount to war crimes. These tensions have added to concerns about a fourth war breaking out between Hamas-led Palestine and Israel. There have been three wars fought between Arabs and Israelis since 1948. A US-led plan is said to be on the drawing board to build outlines for a permanent settlement to the dispute over the territory which was created in 1948 by sending five million Palestinian refugees into exile as a new country was carved out by force in the Middle East. There are no details on what this peace plan may entail. This is an issue that has divided the world now for seven decades. The basic acceptance of the fact that Israel was created against the will of the Palestinians is occluded by the fact that the territory of Israel is considered a holy place by Jews, Christians and Muslims. How can that be any excuse for denying the Palestinians their rights? Their land was stolen from them over 70 years ago. They deserve for it to be returned – and most importantly of all for peace to be given back to them within a territory that they can once again call home.

Created on 2019-05-25 by the reprex package (v0.3.0)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.