Regex force group order











up vote
0
down vote

favorite












I'm new in regex and I have a question.
Like in this example, https://regex101.com/r/Iak7cF/1/ how do I force



src="wow"


to be in group 1, and



title="toto"   


to be in group 2?



I want to capture this kind of text in any order only if it contains:



class="formula"


Am I doing it right?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm new in regex and I have a question.
    Like in this example, https://regex101.com/r/Iak7cF/1/ how do I force



    src="wow"


    to be in group 1, and



    title="toto"   


    to be in group 2?



    I want to capture this kind of text in any order only if it contains:



    class="formula"


    Am I doing it right?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm new in regex and I have a question.
      Like in this example, https://regex101.com/r/Iak7cF/1/ how do I force



      src="wow"


      to be in group 1, and



      title="toto"   


      to be in group 2?



      I want to capture this kind of text in any order only if it contains:



      class="formula"


      Am I doing it right?










      share|improve this question















      I'm new in regex and I have a question.
      Like in this example, https://regex101.com/r/Iak7cF/1/ how do I force



      src="wow"


      to be in group 1, and



      title="toto"   


      to be in group 2?



      I want to capture this kind of text in any order only if it contains:



      class="formula"


      Am I doing it right?







      regex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 16:38









      Liam Strilchuk

      12515




      12515










      asked Nov 10 at 16:33









      Plomging

      62




      62
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          -1
          down vote



          accepted










          You'd better use an HTML parser



          But if you really want to use regex, you have to use named groups to achieve what you want.



          <img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>


          DEMO






          share|improve this answer





















          • No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
            – Plomging
            Nov 10 at 17:36












          • @Plomging: Yes, you're right, but it's clearer when you use the named groups.
            – Toto
            Nov 10 at 18:12










          • @downvoter, what's wrong with this answer?
            – Toto
            Nov 11 at 10:47


















          up vote
          -1
          down vote













          Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:




          Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.




          PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:




          So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!







          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%2f53241032%2fregex-force-group-order%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










            You'd better use an HTML parser



            But if you really want to use regex, you have to use named groups to achieve what you want.



            <img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>


            DEMO






            share|improve this answer





















            • No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
              – Plomging
              Nov 10 at 17:36












            • @Plomging: Yes, you're right, but it's clearer when you use the named groups.
              – Toto
              Nov 10 at 18:12










            • @downvoter, what's wrong with this answer?
              – Toto
              Nov 11 at 10:47















            up vote
            -1
            down vote



            accepted










            You'd better use an HTML parser



            But if you really want to use regex, you have to use named groups to achieve what you want.



            <img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>


            DEMO






            share|improve this answer





















            • No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
              – Plomging
              Nov 10 at 17:36












            • @Plomging: Yes, you're right, but it's clearer when you use the named groups.
              – Toto
              Nov 10 at 18:12










            • @downvoter, what's wrong with this answer?
              – Toto
              Nov 11 at 10:47













            up vote
            -1
            down vote



            accepted







            up vote
            -1
            down vote



            accepted






            You'd better use an HTML parser



            But if you really want to use regex, you have to use named groups to achieve what you want.



            <img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>


            DEMO






            share|improve this answer












            You'd better use an HTML parser



            But if you really want to use regex, you have to use named groups to achieve what you want.



            <img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>


            DEMO







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 17:19









            Toto

            63.8k175697




            63.8k175697












            • No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
              – Plomging
              Nov 10 at 17:36












            • @Plomging: Yes, you're right, but it's clearer when you use the named groups.
              – Toto
              Nov 10 at 18:12










            • @downvoter, what's wrong with this answer?
              – Toto
              Nov 11 at 10:47


















            • No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
              – Plomging
              Nov 10 at 17:36












            • @Plomging: Yes, you're right, but it's clearer when you use the named groups.
              – Toto
              Nov 10 at 18:12










            • @downvoter, what's wrong with this answer?
              – Toto
              Nov 11 at 10:47
















            No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
            – Plomging
            Nov 10 at 17:36






            No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
            – Plomging
            Nov 10 at 17:36














            @Plomging: Yes, you're right, but it's clearer when you use the named groups.
            – Toto
            Nov 10 at 18:12




            @Plomging: Yes, you're right, but it's clearer when you use the named groups.
            – Toto
            Nov 10 at 18:12












            @downvoter, what's wrong with this answer?
            – Toto
            Nov 11 at 10:47




            @downvoter, what's wrong with this answer?
            – Toto
            Nov 11 at 10:47












            up vote
            -1
            down vote













            Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:




            Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.




            PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:




            So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!







            share|improve this answer

























              up vote
              -1
              down vote













              Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:




              Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.




              PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:




              So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!







              share|improve this answer























                up vote
                -1
                down vote










                up vote
                -1
                down vote









                Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:




                Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.




                PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:




                So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!







                share|improve this answer












                Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:




                Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.




                PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:




                So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 17:05









                bitinerant

                463




                463






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241032%2fregex-force-group-order%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma