Splitting Strings based on capital letters (R)












3















I'm extracting song lyrics with Vagalume's API.



library(vagalumeR)
library(tibble)
library(stringr)
set.seed(1234)

key = "7a67715b6d0dd6edf7556cc0d176e4ac"

musicas = as.tibble(topLyrics(name = "seu-jorge",
message = TRUE))

musica = sample(musicas$id.top, 1)

letra = lyrics(identifier = musica,
type = "id",
artist = "seu-jorge",
key = key)


However, letra is just one big block of text and I want to split it into smaller ones



str_split(string = as.character(letra),
"[[:upper:]]")


And this is what I get:



[1] "Pretinha"                                 "aço tudo pelo nosso amor"                
[3] "aço tudo pelo bem de nosso bem (meu bem)" " saudade é minha dor"
[5] "ue anda arrasando com meu coração" "ão"
[7] "uvide que um dia" "u te darei o céu"
[9] "eu amor junto com um anel" "ra gente se casar"


I'm pretty sure most of you don't know portuguese, but trust me, it's skipping the capital letter which I'm using as a separator. How do I include the capital letter into the smaller chunks?










share|improve this question



























    3















    I'm extracting song lyrics with Vagalume's API.



    library(vagalumeR)
    library(tibble)
    library(stringr)
    set.seed(1234)

    key = "7a67715b6d0dd6edf7556cc0d176e4ac"

    musicas = as.tibble(topLyrics(name = "seu-jorge",
    message = TRUE))

    musica = sample(musicas$id.top, 1)

    letra = lyrics(identifier = musica,
    type = "id",
    artist = "seu-jorge",
    key = key)


    However, letra is just one big block of text and I want to split it into smaller ones



    str_split(string = as.character(letra),
    "[[:upper:]]")


    And this is what I get:



    [1] "Pretinha"                                 "aço tudo pelo nosso amor"                
    [3] "aço tudo pelo bem de nosso bem (meu bem)" " saudade é minha dor"
    [5] "ue anda arrasando com meu coração" "ão"
    [7] "uvide que um dia" "u te darei o céu"
    [9] "eu amor junto com um anel" "ra gente se casar"


    I'm pretty sure most of you don't know portuguese, but trust me, it's skipping the capital letter which I'm using as a separator. How do I include the capital letter into the smaller chunks?










    share|improve this question

























      3












      3








      3








      I'm extracting song lyrics with Vagalume's API.



      library(vagalumeR)
      library(tibble)
      library(stringr)
      set.seed(1234)

      key = "7a67715b6d0dd6edf7556cc0d176e4ac"

      musicas = as.tibble(topLyrics(name = "seu-jorge",
      message = TRUE))

      musica = sample(musicas$id.top, 1)

      letra = lyrics(identifier = musica,
      type = "id",
      artist = "seu-jorge",
      key = key)


      However, letra is just one big block of text and I want to split it into smaller ones



      str_split(string = as.character(letra),
      "[[:upper:]]")


      And this is what I get:



      [1] "Pretinha"                                 "aço tudo pelo nosso amor"                
      [3] "aço tudo pelo bem de nosso bem (meu bem)" " saudade é minha dor"
      [5] "ue anda arrasando com meu coração" "ão"
      [7] "uvide que um dia" "u te darei o céu"
      [9] "eu amor junto com um anel" "ra gente se casar"


      I'm pretty sure most of you don't know portuguese, but trust me, it's skipping the capital letter which I'm using as a separator. How do I include the capital letter into the smaller chunks?










      share|improve this question














      I'm extracting song lyrics with Vagalume's API.



      library(vagalumeR)
      library(tibble)
      library(stringr)
      set.seed(1234)

      key = "7a67715b6d0dd6edf7556cc0d176e4ac"

      musicas = as.tibble(topLyrics(name = "seu-jorge",
      message = TRUE))

      musica = sample(musicas$id.top, 1)

      letra = lyrics(identifier = musica,
      type = "id",
      artist = "seu-jorge",
      key = key)


      However, letra is just one big block of text and I want to split it into smaller ones



      str_split(string = as.character(letra),
      "[[:upper:]]")


      And this is what I get:



      [1] "Pretinha"                                 "aço tudo pelo nosso amor"                
      [3] "aço tudo pelo bem de nosso bem (meu bem)" " saudade é minha dor"
      [5] "ue anda arrasando com meu coração" "ão"
      [7] "uvide que um dia" "u te darei o céu"
      [9] "eu amor junto com um anel" "ra gente se casar"


      I'm pretty sure most of you don't know portuguese, but trust me, it's skipping the capital letter which I'm using as a separator. How do I include the capital letter into the smaller chunks?







      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 1:01









      Pedro Cavalcante OliveiraPedro Cavalcante Oliveira

      1347




      1347
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You want to use positive lookahead:



          str_split(string = as.character(letra), "(?=[[:upper:]])")


          It splits at "" if right after it there is a capital letter.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974537%2fsplitting-strings-based-on-capital-letters-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You want to use positive lookahead:



            str_split(string = as.character(letra), "(?=[[:upper:]])")


            It splits at "" if right after it there is a capital letter.






            share|improve this answer




























              2














              You want to use positive lookahead:



              str_split(string = as.character(letra), "(?=[[:upper:]])")


              It splits at "" if right after it there is a capital letter.






              share|improve this answer


























                2












                2








                2







                You want to use positive lookahead:



                str_split(string = as.character(letra), "(?=[[:upper:]])")


                It splits at "" if right after it there is a capital letter.






                share|improve this answer













                You want to use positive lookahead:



                str_split(string = as.character(letra), "(?=[[:upper:]])")


                It splits at "" if right after it there is a capital letter.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 30 '18 at 1:08









                Julius VainoraJulius Vainora

                35.5k76179




                35.5k76179






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974537%2fsplitting-strings-based-on-capital-letters-r%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas

                    Can't read property showImagePicker of undefined in react native iOS