How to to transform x^(…) to x^{…} even when brackets are inside? Pure Regex possible?












0














I need to change many posts and edit the content so that ^(...) becomes x^{...}.



What I have so far:



    $regexpattern = "/^((.*?))/";
$replaceregex = "^{$1}";

$content_new = preg_replace($regexpattern, $replaceregex, $content);


which works.



However, I realized that if there is a round bracket inside the round wrapping brackets, it fails.



Example:



this should be transformed a^(x²) and this b^(-(x²))


becomes:



this should be transformed a^{x²} and this b^{-(x²})


Needed:



this should be transformed a^(x²) and this b^{-(x²)}


How can I prevent this and only replace the last bracket? Or do we need to use PHP and explode the content with ^(...) into Arrays and replace?



It could also be that there are multiple brackets inside (even if rare).



PS: After writing this question I found: https://stackoverflow.com/a/27052437/1066234 but there is no solution provided for this specific case.










share|improve this question
























  • Regular expressions typically have problems with all kinds of nested structures.
    – Some programmer dude
    Nov 12 '18 at 8:15










  • What is expected output for this should be transformed a^(x²) and this b^(-(x²))?
    – anubhava
    Nov 12 '18 at 8:22






  • 1




    this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
    – Nahuel Fouilleul
    Nov 12 '18 at 8:25


















0














I need to change many posts and edit the content so that ^(...) becomes x^{...}.



What I have so far:



    $regexpattern = "/^((.*?))/";
$replaceregex = "^{$1}";

$content_new = preg_replace($regexpattern, $replaceregex, $content);


which works.



However, I realized that if there is a round bracket inside the round wrapping brackets, it fails.



Example:



this should be transformed a^(x²) and this b^(-(x²))


becomes:



this should be transformed a^{x²} and this b^{-(x²})


Needed:



this should be transformed a^(x²) and this b^{-(x²)}


How can I prevent this and only replace the last bracket? Or do we need to use PHP and explode the content with ^(...) into Arrays and replace?



It could also be that there are multiple brackets inside (even if rare).



PS: After writing this question I found: https://stackoverflow.com/a/27052437/1066234 but there is no solution provided for this specific case.










share|improve this question
























  • Regular expressions typically have problems with all kinds of nested structures.
    – Some programmer dude
    Nov 12 '18 at 8:15










  • What is expected output for this should be transformed a^(x²) and this b^(-(x²))?
    – anubhava
    Nov 12 '18 at 8:22






  • 1




    this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
    – Nahuel Fouilleul
    Nov 12 '18 at 8:25
















0












0








0







I need to change many posts and edit the content so that ^(...) becomes x^{...}.



What I have so far:



    $regexpattern = "/^((.*?))/";
$replaceregex = "^{$1}";

$content_new = preg_replace($regexpattern, $replaceregex, $content);


which works.



However, I realized that if there is a round bracket inside the round wrapping brackets, it fails.



Example:



this should be transformed a^(x²) and this b^(-(x²))


becomes:



this should be transformed a^{x²} and this b^{-(x²})


Needed:



this should be transformed a^(x²) and this b^{-(x²)}


How can I prevent this and only replace the last bracket? Or do we need to use PHP and explode the content with ^(...) into Arrays and replace?



It could also be that there are multiple brackets inside (even if rare).



PS: After writing this question I found: https://stackoverflow.com/a/27052437/1066234 but there is no solution provided for this specific case.










share|improve this question















I need to change many posts and edit the content so that ^(...) becomes x^{...}.



What I have so far:



    $regexpattern = "/^((.*?))/";
$replaceregex = "^{$1}";

$content_new = preg_replace($regexpattern, $replaceregex, $content);


which works.



However, I realized that if there is a round bracket inside the round wrapping brackets, it fails.



Example:



this should be transformed a^(x²) and this b^(-(x²))


becomes:



this should be transformed a^{x²} and this b^{-(x²})


Needed:



this should be transformed a^(x²) and this b^{-(x²)}


How can I prevent this and only replace the last bracket? Or do we need to use PHP and explode the content with ^(...) into Arrays and replace?



It could also be that there are multiple brackets inside (even if rare).



PS: After writing this question I found: https://stackoverflow.com/a/27052437/1066234 but there is no solution provided for this specific case.







php regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 8:28









anubhava

520k46316390




520k46316390










asked Nov 12 '18 at 8:12









Kai Noack

6,189659115




6,189659115












  • Regular expressions typically have problems with all kinds of nested structures.
    – Some programmer dude
    Nov 12 '18 at 8:15










  • What is expected output for this should be transformed a^(x²) and this b^(-(x²))?
    – anubhava
    Nov 12 '18 at 8:22






  • 1




    this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
    – Nahuel Fouilleul
    Nov 12 '18 at 8:25




















  • Regular expressions typically have problems with all kinds of nested structures.
    – Some programmer dude
    Nov 12 '18 at 8:15










  • What is expected output for this should be transformed a^(x²) and this b^(-(x²))?
    – anubhava
    Nov 12 '18 at 8:22






  • 1




    this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
    – Nahuel Fouilleul
    Nov 12 '18 at 8:25


















Regular expressions typically have problems with all kinds of nested structures.
– Some programmer dude
Nov 12 '18 at 8:15




Regular expressions typically have problems with all kinds of nested structures.
– Some programmer dude
Nov 12 '18 at 8:15












What is expected output for this should be transformed a^(x²) and this b^(-(x²))?
– anubhava
Nov 12 '18 at 8:22




What is expected output for this should be transformed a^(x²) and this b^(-(x²))?
– anubhava
Nov 12 '18 at 8:22




1




1




this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
– Nahuel Fouilleul
Nov 12 '18 at 8:25






this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
– Nahuel Fouilleul
Nov 12 '18 at 8:25














2 Answers
2






active

oldest

votes


















1














Such regex matches only outter brackets



 ^(((([^()]|(?1))*)))


and replace with



^{$2}


demo on regex101



demo on sandbox






share|improve this answer























  • OP wants to match ^ before (
    – anubhava
    Nov 12 '18 at 8:38






  • 1




    @anubhava Thanks, updated
    – splash58
    Nov 12 '18 at 8:41










  • Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
    – Kai Noack
    Nov 12 '18 at 10:12










  • Glad to help! Good luck!
    – splash58
    Nov 12 '18 at 10:13



















1














You can use this recursive regex in PHP with a negative lookahead:



$str = 'this should be transformed a^(x²) and this b^(-(x²))';
$re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
$repl = preg_replace($re, '^{$2}', $str);
//=> this should be transformed a^(x²) and this b^{-(x²)}


RegEx Demo






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',
    autoActivateHeartbeat: false,
    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%2f53258096%2fhow-to-to-transform-x-to-x-even-when-brackets-are-inside-pure-regex%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









    1














    Such regex matches only outter brackets



     ^(((([^()]|(?1))*)))


    and replace with



    ^{$2}


    demo on regex101



    demo on sandbox






    share|improve this answer























    • OP wants to match ^ before (
      – anubhava
      Nov 12 '18 at 8:38






    • 1




      @anubhava Thanks, updated
      – splash58
      Nov 12 '18 at 8:41










    • Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
      – Kai Noack
      Nov 12 '18 at 10:12










    • Glad to help! Good luck!
      – splash58
      Nov 12 '18 at 10:13
















    1














    Such regex matches only outter brackets



     ^(((([^()]|(?1))*)))


    and replace with



    ^{$2}


    demo on regex101



    demo on sandbox






    share|improve this answer























    • OP wants to match ^ before (
      – anubhava
      Nov 12 '18 at 8:38






    • 1




      @anubhava Thanks, updated
      – splash58
      Nov 12 '18 at 8:41










    • Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
      – Kai Noack
      Nov 12 '18 at 10:12










    • Glad to help! Good luck!
      – splash58
      Nov 12 '18 at 10:13














    1












    1








    1






    Such regex matches only outter brackets



     ^(((([^()]|(?1))*)))


    and replace with



    ^{$2}


    demo on regex101



    demo on sandbox






    share|improve this answer














    Such regex matches only outter brackets



     ^(((([^()]|(?1))*)))


    and replace with



    ^{$2}


    demo on regex101



    demo on sandbox







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 '18 at 9:14

























    answered Nov 12 '18 at 8:36









    splash58

    21k21225




    21k21225












    • OP wants to match ^ before (
      – anubhava
      Nov 12 '18 at 8:38






    • 1




      @anubhava Thanks, updated
      – splash58
      Nov 12 '18 at 8:41










    • Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
      – Kai Noack
      Nov 12 '18 at 10:12










    • Glad to help! Good luck!
      – splash58
      Nov 12 '18 at 10:13


















    • OP wants to match ^ before (
      – anubhava
      Nov 12 '18 at 8:38






    • 1




      @anubhava Thanks, updated
      – splash58
      Nov 12 '18 at 8:41










    • Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
      – Kai Noack
      Nov 12 '18 at 10:12










    • Glad to help! Good luck!
      – splash58
      Nov 12 '18 at 10:13
















    OP wants to match ^ before (
    – anubhava
    Nov 12 '18 at 8:38




    OP wants to match ^ before (
    – anubhava
    Nov 12 '18 at 8:38




    1




    1




    @anubhava Thanks, updated
    – splash58
    Nov 12 '18 at 8:41




    @anubhava Thanks, updated
    – splash58
    Nov 12 '18 at 8:41












    Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
    – Kai Noack
    Nov 12 '18 at 10:12




    Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
    – Kai Noack
    Nov 12 '18 at 10:12












    Glad to help! Good luck!
    – splash58
    Nov 12 '18 at 10:13




    Glad to help! Good luck!
    – splash58
    Nov 12 '18 at 10:13













    1














    You can use this recursive regex in PHP with a negative lookahead:



    $str = 'this should be transformed a^(x²) and this b^(-(x²))';
    $re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
    $repl = preg_replace($re, '^{$2}', $str);
    //=> this should be transformed a^(x²) and this b^{-(x²)}


    RegEx Demo






    share|improve this answer


























      1














      You can use this recursive regex in PHP with a negative lookahead:



      $str = 'this should be transformed a^(x²) and this b^(-(x²))';
      $re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
      $repl = preg_replace($re, '^{$2}', $str);
      //=> this should be transformed a^(x²) and this b^{-(x²)}


      RegEx Demo






      share|improve this answer
























        1












        1








        1






        You can use this recursive regex in PHP with a negative lookahead:



        $str = 'this should be transformed a^(x²) and this b^(-(x²))';
        $re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
        $repl = preg_replace($re, '^{$2}', $str);
        //=> this should be transformed a^(x²) and this b^{-(x²)}


        RegEx Demo






        share|improve this answer












        You can use this recursive regex in PHP with a negative lookahead:



        $str = 'this should be transformed a^(x²) and this b^(-(x²))';
        $re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
        $repl = preg_replace($re, '^{$2}', $str);
        //=> this should be transformed a^(x²) and this b^{-(x²)}


        RegEx Demo







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 8:36









        anubhava

        520k46316390




        520k46316390






























            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%2f53258096%2fhow-to-to-transform-x-to-x-even-when-brackets-are-inside-pure-regex%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

            Bicuculline

            さくらももこ