Scala regex for string extraction between . and whitespace











up vote
0
down vote

favorite
1












I have the following string:



val str = "tagged.big AND tagged.medium"


I need to implement regex that will find all assurance of tagged. till the first whitespace or the end of the line. In the current str I expect to extract 2 strings:



tagged.big
tagged.medium


This is my current attempt:



val pattern = "tagged.*\s".r


but it returns:



Some(tagged.big AND )


Could you please show the proper regexp for this case?










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I have the following string:



    val str = "tagged.big AND tagged.medium"


    I need to implement regex that will find all assurance of tagged. till the first whitespace or the end of the line. In the current str I expect to extract 2 strings:



    tagged.big
    tagged.medium


    This is my current attempt:



    val pattern = "tagged.*\s".r


    but it returns:



    Some(tagged.big AND )


    Could you please show the proper regexp for this case?










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have the following string:



      val str = "tagged.big AND tagged.medium"


      I need to implement regex that will find all assurance of tagged. till the first whitespace or the end of the line. In the current str I expect to extract 2 strings:



      tagged.big
      tagged.medium


      This is my current attempt:



      val pattern = "tagged.*\s".r


      but it returns:



      Some(tagged.big AND )


      Could you please show the proper regexp for this case?










      share|improve this question













      I have the following string:



      val str = "tagged.big AND tagged.medium"


      I need to implement regex that will find all assurance of tagged. till the first whitespace or the end of the line. In the current str I expect to extract 2 strings:



      tagged.big
      tagged.medium


      This is my current attempt:



      val pattern = "tagged.*\s".r


      but it returns:



      Some(tagged.big AND )


      Could you please show the proper regexp for this case?







      regex scala






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 14:58









      alexanoid

      6,8351075164




      6,8351075164
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The pattern tagged.S+ should work here. This would match tagged. followed by one or more whitespace characters. Here is a demo:




          Demo



          This is how I would write the pattern. The problem with your current pattern is that the .* is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium, there is no whitespace character which occurs after it. So, we could try using this:



          tagged.*?(?=s|$)


          This also works.






          share|improve this answer




























            up vote
            0
            down vote













            Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:



            val str = "tagged.big AND tagged.medium"

            val pattern = """(tagged.S+)""".r

            pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
            // res1: List[String] = List(tagged.big, tagged.medium)





            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%2f53240172%2fscala-regex-for-string-extraction-between-and-whitespace%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
              1
              down vote



              accepted










              The pattern tagged.S+ should work here. This would match tagged. followed by one or more whitespace characters. Here is a demo:




              Demo



              This is how I would write the pattern. The problem with your current pattern is that the .* is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium, there is no whitespace character which occurs after it. So, we could try using this:



              tagged.*?(?=s|$)


              This also works.






              share|improve this answer

























                up vote
                1
                down vote



                accepted










                The pattern tagged.S+ should work here. This would match tagged. followed by one or more whitespace characters. Here is a demo:




                Demo



                This is how I would write the pattern. The problem with your current pattern is that the .* is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium, there is no whitespace character which occurs after it. So, we could try using this:



                tagged.*?(?=s|$)


                This also works.






                share|improve this answer























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  The pattern tagged.S+ should work here. This would match tagged. followed by one or more whitespace characters. Here is a demo:




                  Demo



                  This is how I would write the pattern. The problem with your current pattern is that the .* is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium, there is no whitespace character which occurs after it. So, we could try using this:



                  tagged.*?(?=s|$)


                  This also works.






                  share|improve this answer












                  The pattern tagged.S+ should work here. This would match tagged. followed by one or more whitespace characters. Here is a demo:




                  Demo



                  This is how I would write the pattern. The problem with your current pattern is that the .* is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium, there is no whitespace character which occurs after it. So, we could try using this:



                  tagged.*?(?=s|$)


                  This also works.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 15:03









                  Tim Biegeleisen

                  208k1379127




                  208k1379127
























                      up vote
                      0
                      down vote













                      Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:



                      val str = "tagged.big AND tagged.medium"

                      val pattern = """(tagged.S+)""".r

                      pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
                      // res1: List[String] = List(tagged.big, tagged.medium)





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:



                        val str = "tagged.big AND tagged.medium"

                        val pattern = """(tagged.S+)""".r

                        pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
                        // res1: List[String] = List(tagged.big, tagged.medium)





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:



                          val str = "tagged.big AND tagged.medium"

                          val pattern = """(tagged.S+)""".r

                          pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
                          // res1: List[String] = List(tagged.big, tagged.medium)





                          share|improve this answer












                          Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:



                          val str = "tagged.big AND tagged.medium"

                          val pattern = """(tagged.S+)""".r

                          pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
                          // res1: List[String] = List(tagged.big, tagged.medium)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 at 16:11









                          Leo C

                          9,3572616




                          9,3572616






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240172%2fscala-regex-for-string-extraction-between-and-whitespace%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

                              Full-time equivalent

                              さくらももこ

                              13 indicted, 8 arrested in Calif. drug cartel investigation