Regex match from right to left









up vote
0
down vote

favorite












I wanted to match something from right to left, below is one of such example.



100abababab3x3x3xx1000morewords


If i want to match something between and last xx and immediate previous ab and get 3x3x3



I tried something like below , but it matches ababab3x3x3



preg_match('/ab(.*?)xx/',$text,$totmat);


Note : please don't recommend strrev.



Above example is just for illustration , all i wanted to do is match from right to left.










share|improve this question



















  • 1




    Cthulhu is coming! Joke apart, it is not actually text but a possibly nested HTML structure - use a parser and appropriate xpath queries instead.
    – Jan
    Nov 9 at 15:51











  • there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
    – Gracie williams
    Nov 9 at 15:55










  • Nope. See, the point is, xpath queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
    – Jan
    Nov 9 at 15:58










  • Yes i understand , please see my edit.
    – Gracie williams
    Nov 9 at 15:59










  • Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
    – Stuart
    Nov 9 at 16:07














up vote
0
down vote

favorite












I wanted to match something from right to left, below is one of such example.



100abababab3x3x3xx1000morewords


If i want to match something between and last xx and immediate previous ab and get 3x3x3



I tried something like below , but it matches ababab3x3x3



preg_match('/ab(.*?)xx/',$text,$totmat);


Note : please don't recommend strrev.



Above example is just for illustration , all i wanted to do is match from right to left.










share|improve this question



















  • 1




    Cthulhu is coming! Joke apart, it is not actually text but a possibly nested HTML structure - use a parser and appropriate xpath queries instead.
    – Jan
    Nov 9 at 15:51











  • there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
    – Gracie williams
    Nov 9 at 15:55










  • Nope. See, the point is, xpath queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
    – Jan
    Nov 9 at 15:58










  • Yes i understand , please see my edit.
    – Gracie williams
    Nov 9 at 15:59










  • Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
    – Stuart
    Nov 9 at 16:07












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I wanted to match something from right to left, below is one of such example.



100abababab3x3x3xx1000morewords


If i want to match something between and last xx and immediate previous ab and get 3x3x3



I tried something like below , but it matches ababab3x3x3



preg_match('/ab(.*?)xx/',$text,$totmat);


Note : please don't recommend strrev.



Above example is just for illustration , all i wanted to do is match from right to left.










share|improve this question















I wanted to match something from right to left, below is one of such example.



100abababab3x3x3xx1000morewords


If i want to match something between and last xx and immediate previous ab and get 3x3x3



I tried something like below , but it matches ababab3x3x3



preg_match('/ab(.*?)xx/',$text,$totmat);


Note : please don't recommend strrev.



Above example is just for illustration , all i wanted to do is match from right to left.







php regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 16:31

























asked Nov 9 at 15:44









Gracie williams

558




558







  • 1




    Cthulhu is coming! Joke apart, it is not actually text but a possibly nested HTML structure - use a parser and appropriate xpath queries instead.
    – Jan
    Nov 9 at 15:51











  • there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
    – Gracie williams
    Nov 9 at 15:55










  • Nope. See, the point is, xpath queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
    – Jan
    Nov 9 at 15:58










  • Yes i understand , please see my edit.
    – Gracie williams
    Nov 9 at 15:59










  • Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
    – Stuart
    Nov 9 at 16:07












  • 1




    Cthulhu is coming! Joke apart, it is not actually text but a possibly nested HTML structure - use a parser and appropriate xpath queries instead.
    – Jan
    Nov 9 at 15:51











  • there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
    – Gracie williams
    Nov 9 at 15:55










  • Nope. See, the point is, xpath queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
    – Jan
    Nov 9 at 15:58










  • Yes i understand , please see my edit.
    – Gracie williams
    Nov 9 at 15:59










  • Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
    – Stuart
    Nov 9 at 16:07







1




1




Cthulhu is coming! Joke apart, it is not actually text but a possibly nested HTML structure - use a parser and appropriate xpath queries instead.
– Jan
Nov 9 at 15:51





Cthulhu is coming! Joke apart, it is not actually text but a possibly nested HTML structure - use a parser and appropriate xpath queries instead.
– Jan
Nov 9 at 15:51













there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
– Gracie williams
Nov 9 at 15:55




there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
– Gracie williams
Nov 9 at 15:55












Nope. See, the point is, xpath queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
– Jan
Nov 9 at 15:58




Nope. See, the point is, xpath queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
– Jan
Nov 9 at 15:58












Yes i understand , please see my edit.
– Gracie williams
Nov 9 at 15:59




Yes i understand , please see my edit.
– Gracie williams
Nov 9 at 15:59












Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
– Stuart
Nov 9 at 16:07




Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
– Stuart
Nov 9 at 16:07












3 Answers
3






active

oldest

votes

















up vote
1
down vote













Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=) and Look behind positive (?<=). See regex



<?php

$re = '/w+(?<=ab)(.*?)(?=xx)/m';
$str = '100abababab3x3x3xx1000morewords';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the full matched result
echo $matches[0][1];


DEMO: https://3v4l.org/db69N






share|improve this answer





























    up vote
    0
    down vote













    $str = '100abababab3x3x3xx1000morewords';
    preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
    print_r($m);


    Output:



    Array
    (
    [0] => ab3x3x3xx
    [1] => 3x3x3
    )


    >Explanation:



    ab : literally ab
    ( : start group 1
    (?: : start non capture group
    (?!ab) : negative lookahead, make sure we doon't have ab
    . : any character but newline
    )* : end group, may appear 0 or more times
    ) : end group 1
    xx : literally xx





    share|improve this answer



























      up vote
      0
      down vote













      There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.



      Here for example :



      $str = "100abababab3x3x3xx1000morewords";
      $result = explode("ab", explode("xx", $str)[0]);
      var_dump(end($result));


      First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).



      Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result)); prints the expected result.






      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%2f53228926%2fregex-match-from-right-to-left%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=) and Look behind positive (?<=). See regex



        <?php

        $re = '/w+(?<=ab)(.*?)(?=xx)/m';
        $str = '100abababab3x3x3xx1000morewords';

        preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

        // Print the full matched result
        echo $matches[0][1];


        DEMO: https://3v4l.org/db69N






        share|improve this answer


























          up vote
          1
          down vote













          Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=) and Look behind positive (?<=). See regex



          <?php

          $re = '/w+(?<=ab)(.*?)(?=xx)/m';
          $str = '100abababab3x3x3xx1000morewords';

          preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

          // Print the full matched result
          echo $matches[0][1];


          DEMO: https://3v4l.org/db69N






          share|improve this answer
























            up vote
            1
            down vote










            up vote
            1
            down vote









            Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=) and Look behind positive (?<=). See regex



            <?php

            $re = '/w+(?<=ab)(.*?)(?=xx)/m';
            $str = '100abababab3x3x3xx1000morewords';

            preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

            // Print the full matched result
            echo $matches[0][1];


            DEMO: https://3v4l.org/db69N






            share|improve this answer














            Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=) and Look behind positive (?<=). See regex



            <?php

            $re = '/w+(?<=ab)(.*?)(?=xx)/m';
            $str = '100abababab3x3x3xx1000morewords';

            preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

            // Print the full matched result
            echo $matches[0][1];


            DEMO: https://3v4l.org/db69N







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 8:05

























            answered Nov 9 at 16:38









            Curious_MInd

            13.2k32340




            13.2k32340






















                up vote
                0
                down vote













                $str = '100abababab3x3x3xx1000morewords';
                preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
                print_r($m);


                Output:



                Array
                (
                [0] => ab3x3x3xx
                [1] => 3x3x3
                )


                >Explanation:



                ab : literally ab
                ( : start group 1
                (?: : start non capture group
                (?!ab) : negative lookahead, make sure we doon't have ab
                . : any character but newline
                )* : end group, may appear 0 or more times
                ) : end group 1
                xx : literally xx





                share|improve this answer
























                  up vote
                  0
                  down vote













                  $str = '100abababab3x3x3xx1000morewords';
                  preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
                  print_r($m);


                  Output:



                  Array
                  (
                  [0] => ab3x3x3xx
                  [1] => 3x3x3
                  )


                  >Explanation:



                  ab : literally ab
                  ( : start group 1
                  (?: : start non capture group
                  (?!ab) : negative lookahead, make sure we doon't have ab
                  . : any character but newline
                  )* : end group, may appear 0 or more times
                  ) : end group 1
                  xx : literally xx





                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    $str = '100abababab3x3x3xx1000morewords';
                    preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
                    print_r($m);


                    Output:



                    Array
                    (
                    [0] => ab3x3x3xx
                    [1] => 3x3x3
                    )


                    >Explanation:



                    ab : literally ab
                    ( : start group 1
                    (?: : start non capture group
                    (?!ab) : negative lookahead, make sure we doon't have ab
                    . : any character but newline
                    )* : end group, may appear 0 or more times
                    ) : end group 1
                    xx : literally xx





                    share|improve this answer












                    $str = '100abababab3x3x3xx1000morewords';
                    preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
                    print_r($m);


                    Output:



                    Array
                    (
                    [0] => ab3x3x3xx
                    [1] => 3x3x3
                    )


                    >Explanation:



                    ab : literally ab
                    ( : start group 1
                    (?: : start non capture group
                    (?!ab) : negative lookahead, make sure we doon't have ab
                    . : any character but newline
                    )* : end group, may appear 0 or more times
                    ) : end group 1
                    xx : literally xx






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 16:52









                    Toto

                    63.8k175697




                    63.8k175697




















                        up vote
                        0
                        down vote













                        There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.



                        Here for example :



                        $str = "100abababab3x3x3xx1000morewords";
                        $result = explode("ab", explode("xx", $str)[0]);
                        var_dump(end($result));


                        First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).



                        Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result)); prints the expected result.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.



                          Here for example :



                          $str = "100abababab3x3x3xx1000morewords";
                          $result = explode("ab", explode("xx", $str)[0]);
                          var_dump(end($result));


                          First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).



                          Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result)); prints the expected result.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.



                            Here for example :



                            $str = "100abababab3x3x3xx1000morewords";
                            $result = explode("ab", explode("xx", $str)[0]);
                            var_dump(end($result));


                            First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).



                            Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result)); prints the expected result.






                            share|improve this answer












                            There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.



                            Here for example :



                            $str = "100abababab3x3x3xx1000morewords";
                            $result = explode("ab", explode("xx", $str)[0]);
                            var_dump(end($result));


                            First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).



                            Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result)); prints the expected result.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 9 at 17:07









                            Lou

                            676213




                            676213



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228926%2fregex-match-from-right-to-left%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

                                How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                Syphilis

                                Darth Vader #20