How to get a string in regex and delete other after matching the string









up vote
0
down vote

favorite












my input is following



1 blah blah blah @username_. sblah sblah sblah



the output I need is following
username_.



for now, I make this expression



^.*@([a-zA-Z0-9._]+)$



which working in following



1 blah blah blah @username_.



but if I use it for the full line it's not working



so its get the user and delete before the user
but how I can make it delete the rest once it gets the user



Note I use regex101 for testing if you have a better tool please write it below.










share|improve this question





















  • It will always end with _.?
    – Juan Ignacio Sánchez
    yesterday










  • You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
    – ryantxr
    yesterday















up vote
0
down vote

favorite












my input is following



1 blah blah blah @username_. sblah sblah sblah



the output I need is following
username_.



for now, I make this expression



^.*@([a-zA-Z0-9._]+)$



which working in following



1 blah blah blah @username_.



but if I use it for the full line it's not working



so its get the user and delete before the user
but how I can make it delete the rest once it gets the user



Note I use regex101 for testing if you have a better tool please write it below.










share|improve this question





















  • It will always end with _.?
    – Juan Ignacio Sánchez
    yesterday










  • You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
    – ryantxr
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











my input is following



1 blah blah blah @username_. sblah sblah sblah



the output I need is following
username_.



for now, I make this expression



^.*@([a-zA-Z0-9._]+)$



which working in following



1 blah blah blah @username_.



but if I use it for the full line it's not working



so its get the user and delete before the user
but how I can make it delete the rest once it gets the user



Note I use regex101 for testing if you have a better tool please write it below.










share|improve this question













my input is following



1 blah blah blah @username_. sblah sblah sblah



the output I need is following
username_.



for now, I make this expression



^.*@([a-zA-Z0-9._]+)$



which working in following



1 blah blah blah @username_.



but if I use it for the full line it's not working



so its get the user and delete before the user
but how I can make it delete the rest once it gets the user



Note I use regex101 for testing if you have a better tool please write it below.







php regex instagram-api






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









TheBlueDragon

5815




5815











  • It will always end with _.?
    – Juan Ignacio Sánchez
    yesterday










  • You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
    – ryantxr
    yesterday

















  • It will always end with _.?
    – Juan Ignacio Sánchez
    yesterday










  • You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
    – ryantxr
    yesterday
















It will always end with _.?
– Juan Ignacio Sánchez
yesterday




It will always end with _.?
– Juan Ignacio Sánchez
yesterday












You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
– ryantxr
yesterday





You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
– ryantxr
yesterday













2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Your pattern uses ^$ which means it needs a full match, your pattern is only partial.

By adding a .* it becomes a full regex and it matches as expected.



"/^.*@([a-zA-Z0-9._]+).*$/"


https://3v4l.org/i4pVd



Another way to do it is to use a partial regex like this.

It skips anything up to the @ and then captures all to a dot



$str = "1 blah blah blah @username_. sblah sblah sblah";
preg_match("/.*?@(.*?.)/", $str, $match);

var_dump($match);


https://3v4l.org/mvBYI






share|improve this answer





























    up vote
    0
    down vote













    To match the username in you example data, you could preg_match and omit the $ to assert the position at the end of the string as in this demo. Note that you don't have to escape the @ and the dot and underscore in the character class.



    To get the username in you example data, you could also use:



    @K[w.]+



    That would match




    • @ Match literally


    • K Forget what was previously matched


    • [w.]+ Match 1+ times a word character or a dot

    Regex demo



    $re = '/@K[w.]+/';
    $str = '1 blah blah blah @username_. sblah sblah sblah @test';
    preg_match($re, $str, $matches);
    echo $matches[0]; // username_.


    Demo php






    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%2f53236460%2fhow-to-get-a-string-in-regex-and-delete-other-after-matching-the-string%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Your pattern uses ^$ which means it needs a full match, your pattern is only partial.

      By adding a .* it becomes a full regex and it matches as expected.



      "/^.*@([a-zA-Z0-9._]+).*$/"


      https://3v4l.org/i4pVd



      Another way to do it is to use a partial regex like this.

      It skips anything up to the @ and then captures all to a dot



      $str = "1 blah blah blah @username_. sblah sblah sblah";
      preg_match("/.*?@(.*?.)/", $str, $match);

      var_dump($match);


      https://3v4l.org/mvBYI






      share|improve this answer


























        up vote
        1
        down vote



        accepted










        Your pattern uses ^$ which means it needs a full match, your pattern is only partial.

        By adding a .* it becomes a full regex and it matches as expected.



        "/^.*@([a-zA-Z0-9._]+).*$/"


        https://3v4l.org/i4pVd



        Another way to do it is to use a partial regex like this.

        It skips anything up to the @ and then captures all to a dot



        $str = "1 blah blah blah @username_. sblah sblah sblah";
        preg_match("/.*?@(.*?.)/", $str, $match);

        var_dump($match);


        https://3v4l.org/mvBYI






        share|improve this answer
























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Your pattern uses ^$ which means it needs a full match, your pattern is only partial.

          By adding a .* it becomes a full regex and it matches as expected.



          "/^.*@([a-zA-Z0-9._]+).*$/"


          https://3v4l.org/i4pVd



          Another way to do it is to use a partial regex like this.

          It skips anything up to the @ and then captures all to a dot



          $str = "1 blah blah blah @username_. sblah sblah sblah";
          preg_match("/.*?@(.*?.)/", $str, $match);

          var_dump($match);


          https://3v4l.org/mvBYI






          share|improve this answer














          Your pattern uses ^$ which means it needs a full match, your pattern is only partial.

          By adding a .* it becomes a full regex and it matches as expected.



          "/^.*@([a-zA-Z0-9._]+).*$/"


          https://3v4l.org/i4pVd



          Another way to do it is to use a partial regex like this.

          It skips anything up to the @ and then captures all to a dot



          $str = "1 blah blah blah @username_. sblah sblah sblah";
          preg_match("/.*?@(.*?.)/", $str, $match);

          var_dump($match);


          https://3v4l.org/mvBYI







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Andreas

          14.3k31440




          14.3k31440






















              up vote
              0
              down vote













              To match the username in you example data, you could preg_match and omit the $ to assert the position at the end of the string as in this demo. Note that you don't have to escape the @ and the dot and underscore in the character class.



              To get the username in you example data, you could also use:



              @K[w.]+



              That would match




              • @ Match literally


              • K Forget what was previously matched


              • [w.]+ Match 1+ times a word character or a dot

              Regex demo



              $re = '/@K[w.]+/';
              $str = '1 blah blah blah @username_. sblah sblah sblah @test';
              preg_match($re, $str, $matches);
              echo $matches[0]; // username_.


              Demo php






              share|improve this answer


























                up vote
                0
                down vote













                To match the username in you example data, you could preg_match and omit the $ to assert the position at the end of the string as in this demo. Note that you don't have to escape the @ and the dot and underscore in the character class.



                To get the username in you example data, you could also use:



                @K[w.]+



                That would match




                • @ Match literally


                • K Forget what was previously matched


                • [w.]+ Match 1+ times a word character or a dot

                Regex demo



                $re = '/@K[w.]+/';
                $str = '1 blah blah blah @username_. sblah sblah sblah @test';
                preg_match($re, $str, $matches);
                echo $matches[0]; // username_.


                Demo php






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  To match the username in you example data, you could preg_match and omit the $ to assert the position at the end of the string as in this demo. Note that you don't have to escape the @ and the dot and underscore in the character class.



                  To get the username in you example data, you could also use:



                  @K[w.]+



                  That would match




                  • @ Match literally


                  • K Forget what was previously matched


                  • [w.]+ Match 1+ times a word character or a dot

                  Regex demo



                  $re = '/@K[w.]+/';
                  $str = '1 blah blah blah @username_. sblah sblah sblah @test';
                  preg_match($re, $str, $matches);
                  echo $matches[0]; // username_.


                  Demo php






                  share|improve this answer














                  To match the username in you example data, you could preg_match and omit the $ to assert the position at the end of the string as in this demo. Note that you don't have to escape the @ and the dot and underscore in the character class.



                  To get the username in you example data, you could also use:



                  @K[w.]+



                  That would match




                  • @ Match literally


                  • K Forget what was previously matched


                  • [w.]+ Match 1+ times a word character or a dot

                  Regex demo



                  $re = '/@K[w.]+/';
                  $str = '1 blah blah blah @username_. sblah sblah sblah @test';
                  preg_match($re, $str, $matches);
                  echo $matches[0]; // username_.


                  Demo php







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  The fourth bird

                  17.9k71323




                  17.9k71323



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236460%2fhow-to-get-a-string-in-regex-and-delete-other-after-matching-the-string%23new-answer', 'question_page');

                      );

                      Post as a guest














































































                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      Evgeni Malkin