简体   繁体   中英

Want to remove all characters in a string before a specific multi-word character

I have the following gastly string:

x <- "Tomas Ceresnak (C)\nC 71 16 Elko Prairie Cowboys\n- UNK\nVratislav Bohácik (F)\nF 71 16 Wabun Huskies\n- UNK\nLuca Mullins (D)\nD 71 16 Groundbirch Rhino Chuckers\n- UNK\nDandre Carlton (F)\nF 71 16 Ebony Gothic Knights\n- UNK\nLynn Marez (F)\nF 71 16 Ebony Gothic Knights\n- UNK\nHynek Hoško (C)\nC 71 16 HC Kometa Železnice U18\n- UNK\nGlynn Shields (F)\nF 71 16 Chanhassen Nova Ocelots\n- UNK\nJeet Beals (C)\nC 71 16 Chanhassen Nova Ocelots\n- UNK\nVeit Olivarez (F)\nF 71 16 Minnesota City Electricity\n- UNK\nGregory Mason (D)\nD 71 16 McMurphy Energy\n- UNK\nElias Storck (C)\nC 71 16 SK Semla U18\n- UNK\nKnut Scheutz (C)\nC 71 16 Bogla AIF U18\n- UNK\nJonny Hendrix (F)\nF 71 16 Minnesota City Electricity\n- UNK\nDmitry Kuvayev (G)\nG 71 16 Rotor Pervomayskiy U18\n- UNK\nKofi Orona (G)\nG 71 16 Cherhill Vikes\n- UNK"

I want to remove everything before and including "Dandre Carlton (F)" which can be found at the end of the second line. I'm a pretty poor coder but this is apart of a webs craping project I'm trying to implement. Essentially, my information is spread across two pages and breaks at the specific individual Dandre Carlton. I'm then counting how many individuals occur after Dandre Carlton by using str_count("[(]",string) to get a total count of individuals, as I can identify a new individual from the occurrence of a left parenthesis.

I have "Dandre Carlton (F)" stored in a variable called name and the whole string just stored in string. I've tried:

newstring<-gsub(paste0(".*",name),"",string)

but clearly that hasn't worked for me, Again. I need this to be general enough that I can paste whatever name is the divider between the two pages to count those afterwards.

The result I'd like to get is

"\nF 71 16 Ebony Gothic Knights\n- UNK\nLynn Marez (F)\nF 71 16 Ebony Gothic Knights\n- UNK\nHynek Hoško (C)\nC 71 16 HC Kometa Železnice U18\n- UNK\nGlynn Shields (F)\nF 71 16 Chanhassen Nova Ocelots\n- UNK\nJeet Beals (C)\nC 71 16 Chanhassen Nova Ocelots\n- UNK\nVeit Olivarez (F)\nF 71 16 Minnesota City Electricity\n- UNK\nGregory Mason (D)\nD 71 16 McMurphy Energy\n- UNK\nElias Storck (C)\nC 71 16 SK Semla U18\n- UNK\nKnut Scheutz (C)\nC 71 16 Bogla AIF U18\n- UNK\nJonny Hendrix (F)\nF 71 16 Minnesota City Electricity\n- UNK\nDmitry Kuvayev (G)\nG 71 16 Rotor Pervomayskiy U18\n- UNK\nKofi Orona (G)\nG 71 16 Cherhill Vikes\n- UNK"

To which I'll then use:

individuals<-str_count("[(]",newstring)

which gives me the number I'm after

If you are able to save with escaped parentheses ,

name <-  "Dandre Carlton \\(F\\)"

else use stringi .

name <- stringi::stri_replace_all_regex(name, c('\\(', '\\)'), c('\\\\(', '\\\\)'), vectorize_all=F)

Then it's just

gsub(paste0('.*', name), '', x)
[1] "\nF 71 16 Ebony Gothic Knights\n- UNK\nLynn Marez (F)\nF 71 16 Ebony Gothic Knights\n- UNK\nHynek Hoško (C)\nC 71 16 HC Kometa Železnice U18\n- UNK\nGlynn Shields (F)\nF 71 16 Chanhassen Nova Ocelots\n- UNK\nJeet Beals (C)\nC 71 16 Chanhassen Nova Ocelots\n- UNK\nVeit Olivarez (F)\nF 71 16 Minnesota City Electricity\n- UNK\nGregory Mason (D)\nD 71 16 McMurphy Energy\n- UNK\nElias Storck (C)\nC 71 16 SK Semla U18\n- UNK\nKnut Scheutz (C)\nC 71 16 Bogla AIF U18\n- UNK\nJonny Hendrix (F)\nF 71 16 Minnesota City Electricity\n- UNK\nDmitry Kuvayev (G)\nG 71 16 Rotor Pervomayskiy U18\n- UNK\nKofi Orona (G)\nG 71 16 Cherhill Vikes\n- UNK"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM