All first numbers after some string pattern using regex in R











up vote
1
down vote

favorite












I want to extract all numbers after string "mystr" and something else. For example if I have string.



x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."


It should return 8 and 12.
In R I tried:



stringr::str_extract_all(x, "mystr.*\d+")









share|improve this question






















  • This returns only first number, not all of them
    – Mislav
    Nov 8 at 22:06










  • Well it seems you have multiple mystr in your input string. Then you may need to turn your dot-star into its un-greedy version mystr.*?(\d+)
    – revo
    Nov 8 at 22:07

















up vote
1
down vote

favorite












I want to extract all numbers after string "mystr" and something else. For example if I have string.



x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."


It should return 8 and 12.
In R I tried:



stringr::str_extract_all(x, "mystr.*\d+")









share|improve this question






















  • This returns only first number, not all of them
    – Mislav
    Nov 8 at 22:06










  • Well it seems you have multiple mystr in your input string. Then you may need to turn your dot-star into its un-greedy version mystr.*?(\d+)
    – revo
    Nov 8 at 22:07















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to extract all numbers after string "mystr" and something else. For example if I have string.



x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."


It should return 8 and 12.
In R I tried:



stringr::str_extract_all(x, "mystr.*\d+")









share|improve this question













I want to extract all numbers after string "mystr" and something else. For example if I have string.



x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."


It should return 8 and 12.
In R I tried:



stringr::str_extract_all(x, "mystr.*\d+")






r regex stringr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 21:58









Mislav

453318




453318












  • This returns only first number, not all of them
    – Mislav
    Nov 8 at 22:06










  • Well it seems you have multiple mystr in your input string. Then you may need to turn your dot-star into its un-greedy version mystr.*?(\d+)
    – revo
    Nov 8 at 22:07




















  • This returns only first number, not all of them
    – Mislav
    Nov 8 at 22:06










  • Well it seems you have multiple mystr in your input string. Then you may need to turn your dot-star into its un-greedy version mystr.*?(\d+)
    – revo
    Nov 8 at 22:07


















This returns only first number, not all of them
– Mislav
Nov 8 at 22:06




This returns only first number, not all of them
– Mislav
Nov 8 at 22:06












Well it seems you have multiple mystr in your input string. Then you may need to turn your dot-star into its un-greedy version mystr.*?(\d+)
– revo
Nov 8 at 22:07






Well it seems you have multiple mystr in your input string. Then you may need to turn your dot-star into its un-greedy version mystr.*?(\d+)
– revo
Nov 8 at 22:07














2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










You may extract the closest digit chunks after mystr using



x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
# => [[1]]
# [1] "8" "12"


See the R demo



This PCRE regex will match





  • mystr - mystr


  • .*? - any 0+ chars other than line break chars as few as possible


  • \K - will omit the text matched so far


  • \d+ - 1+ digits.


See the PCRE regex demo.



If you want to use stringr, you may use str_match_all:



> library(stringr)
> x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
> str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
[1] "8" "12"


were the digits are captured into Group 1.






share|improve this answer




























    up vote
    1
    down vote













    Sometimes str_match is more flexible than str_extract:



    library(stringr)
    str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
    "mystring.*?(\d+)")[[1]][, 2]

    [1] "8" "12"





    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',
      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%2f53216787%2fall-first-numbers-after-some-string-pattern-using-regex-in-r%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      You may extract the closest digit chunks after mystr using



      x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
      regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
      # => [[1]]
      # [1] "8" "12"


      See the R demo



      This PCRE regex will match





      • mystr - mystr


      • .*? - any 0+ chars other than line break chars as few as possible


      • \K - will omit the text matched so far


      • \d+ - 1+ digits.


      See the PCRE regex demo.



      If you want to use stringr, you may use str_match_all:



      > library(stringr)
      > x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
      > str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
      [1] "8" "12"


      were the digits are captured into Group 1.






      share|improve this answer

























        up vote
        2
        down vote



        accepted










        You may extract the closest digit chunks after mystr using



        x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
        regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
        # => [[1]]
        # [1] "8" "12"


        See the R demo



        This PCRE regex will match





        • mystr - mystr


        • .*? - any 0+ chars other than line break chars as few as possible


        • \K - will omit the text matched so far


        • \d+ - 1+ digits.


        See the PCRE regex demo.



        If you want to use stringr, you may use str_match_all:



        > library(stringr)
        > x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
        > str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
        [1] "8" "12"


        were the digits are captured into Group 1.






        share|improve this answer























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You may extract the closest digit chunks after mystr using



          x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
          regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
          # => [[1]]
          # [1] "8" "12"


          See the R demo



          This PCRE regex will match





          • mystr - mystr


          • .*? - any 0+ chars other than line break chars as few as possible


          • \K - will omit the text matched so far


          • \d+ - 1+ digits.


          See the PCRE regex demo.



          If you want to use stringr, you may use str_match_all:



          > library(stringr)
          > x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
          > str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
          [1] "8" "12"


          were the digits are captured into Group 1.






          share|improve this answer












          You may extract the closest digit chunks after mystr using



          x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
          regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
          # => [[1]]
          # [1] "8" "12"


          See the R demo



          This PCRE regex will match





          • mystr - mystr


          • .*? - any 0+ chars other than line break chars as few as possible


          • \K - will omit the text matched so far


          • \d+ - 1+ digits.


          See the PCRE regex demo.



          If you want to use stringr, you may use str_match_all:



          > library(stringr)
          > x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
          > str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
          [1] "8" "12"


          were the digits are captured into Group 1.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 22:06









          Wiktor Stribiżew

          304k16123200




          304k16123200
























              up vote
              1
              down vote













              Sometimes str_match is more flexible than str_extract:



              library(stringr)
              str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
              "mystring.*?(\d+)")[[1]][, 2]

              [1] "8" "12"





              share|improve this answer

























                up vote
                1
                down vote













                Sometimes str_match is more flexible than str_extract:



                library(stringr)
                str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
                "mystring.*?(\d+)")[[1]][, 2]

                [1] "8" "12"





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Sometimes str_match is more flexible than str_extract:



                  library(stringr)
                  str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
                  "mystring.*?(\d+)")[[1]][, 2]

                  [1] "8" "12"





                  share|improve this answer












                  Sometimes str_match is more flexible than str_extract:



                  library(stringr)
                  str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
                  "mystring.*?(\d+)")[[1]][, 2]

                  [1] "8" "12"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 22:10









                  neilfws

                  17.5k53648




                  17.5k53648






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53216787%2fall-first-numbers-after-some-string-pattern-using-regex-in-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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings