regex how to find and replace at multiple instance along multi line search












0















I have the following text:



vyāsa-prasādāc chrutavānZKL
etad guhyam ahaṁ paramZKL
yogaṁ yogeśvarāt kṛṣṇātZKL
sākṣāt kathayataḥ svayamPXY

rājan saṁsmṛtya saṁsmṛtyaZKL
saṁvādam imam adbhutamZKL
keśavārjunayoḥ puṇyaṁZKL
hṛṣyāmi ca muhur muhuḥPXY

tac ca saṁsmṛtya saṁsmṛtyaZKL
rūpam aty-adbhutaṁ hareḥZKL
vismayo me mahān rājanZKL
hṛṣyāmi ca punaḥ punaḥPXY

yatra yogeśvaraḥ kṛṣṇoZKL
yatra pārtho dhanur-dharaḥZKL
tatra śrīr vijayo bhūtirZKL
dhruvā nītir matir mamaPXY


Now i want:



vyāsa-prasādāc chrutavānZKL
etad guhyam ahaṁ paramZKL
yogaṁ yogeśvarāt kṛṣṇāt sākṣāt kathayataḥ svayamPXY

rājan saṁsmṛtya saṁsmṛtyaZKL
saṁvādam imam adbhutamZKL
keśavārjunayoḥ puṇyaṁ hṛṣyāmi ca muhur muhuḥPXY

tac ca saṁsmṛtya saṁsmṛtyaZKL
rūpam aty-adbhutaṁ hareḥZKL
vismayo me mahān rājan hṛṣyāmi ca punaḥ punaḥPXY

yatra yogeśvaraḥ kṛṣṇoZKL
yatra pārtho dhanur-dharaḥZKL
tatra śrīr vijayo bhūtir dhruvā nītir matir mamaPXY


I want to merge the last two lines in each stanza.



I am trying in sublime text:



find: (?s)ZKLn((?!.*ZKL).*PXY)
replace: 1

(?s) for multi line search
ZKLn is ZKL and new line
((?!.*ZKL) to search from ZKL which is before PXY otherwse there are many ZKL
.*PXY everything till PXY


the above will only replace the last instance. How to see that all four instances in four stanzas are replaced.



i have lot off such stanzas










share|improve this question























  • @ssc-hrep3 dictionary.com/browse/stanza

    – Sweeper
    Nov 24 '18 at 19:30
















0















I have the following text:



vyāsa-prasādāc chrutavānZKL
etad guhyam ahaṁ paramZKL
yogaṁ yogeśvarāt kṛṣṇātZKL
sākṣāt kathayataḥ svayamPXY

rājan saṁsmṛtya saṁsmṛtyaZKL
saṁvādam imam adbhutamZKL
keśavārjunayoḥ puṇyaṁZKL
hṛṣyāmi ca muhur muhuḥPXY

tac ca saṁsmṛtya saṁsmṛtyaZKL
rūpam aty-adbhutaṁ hareḥZKL
vismayo me mahān rājanZKL
hṛṣyāmi ca punaḥ punaḥPXY

yatra yogeśvaraḥ kṛṣṇoZKL
yatra pārtho dhanur-dharaḥZKL
tatra śrīr vijayo bhūtirZKL
dhruvā nītir matir mamaPXY


Now i want:



vyāsa-prasādāc chrutavānZKL
etad guhyam ahaṁ paramZKL
yogaṁ yogeśvarāt kṛṣṇāt sākṣāt kathayataḥ svayamPXY

rājan saṁsmṛtya saṁsmṛtyaZKL
saṁvādam imam adbhutamZKL
keśavārjunayoḥ puṇyaṁ hṛṣyāmi ca muhur muhuḥPXY

tac ca saṁsmṛtya saṁsmṛtyaZKL
rūpam aty-adbhutaṁ hareḥZKL
vismayo me mahān rājan hṛṣyāmi ca punaḥ punaḥPXY

yatra yogeśvaraḥ kṛṣṇoZKL
yatra pārtho dhanur-dharaḥZKL
tatra śrīr vijayo bhūtir dhruvā nītir matir mamaPXY


I want to merge the last two lines in each stanza.



I am trying in sublime text:



find: (?s)ZKLn((?!.*ZKL).*PXY)
replace: 1

(?s) for multi line search
ZKLn is ZKL and new line
((?!.*ZKL) to search from ZKL which is before PXY otherwse there are many ZKL
.*PXY everything till PXY


the above will only replace the last instance. How to see that all four instances in four stanzas are replaced.



i have lot off such stanzas










share|improve this question























  • @ssc-hrep3 dictionary.com/browse/stanza

    – Sweeper
    Nov 24 '18 at 19:30














0












0








0








I have the following text:



vyāsa-prasādāc chrutavānZKL
etad guhyam ahaṁ paramZKL
yogaṁ yogeśvarāt kṛṣṇātZKL
sākṣāt kathayataḥ svayamPXY

rājan saṁsmṛtya saṁsmṛtyaZKL
saṁvādam imam adbhutamZKL
keśavārjunayoḥ puṇyaṁZKL
hṛṣyāmi ca muhur muhuḥPXY

tac ca saṁsmṛtya saṁsmṛtyaZKL
rūpam aty-adbhutaṁ hareḥZKL
vismayo me mahān rājanZKL
hṛṣyāmi ca punaḥ punaḥPXY

yatra yogeśvaraḥ kṛṣṇoZKL
yatra pārtho dhanur-dharaḥZKL
tatra śrīr vijayo bhūtirZKL
dhruvā nītir matir mamaPXY


Now i want:



vyāsa-prasādāc chrutavānZKL
etad guhyam ahaṁ paramZKL
yogaṁ yogeśvarāt kṛṣṇāt sākṣāt kathayataḥ svayamPXY

rājan saṁsmṛtya saṁsmṛtyaZKL
saṁvādam imam adbhutamZKL
keśavārjunayoḥ puṇyaṁ hṛṣyāmi ca muhur muhuḥPXY

tac ca saṁsmṛtya saṁsmṛtyaZKL
rūpam aty-adbhutaṁ hareḥZKL
vismayo me mahān rājan hṛṣyāmi ca punaḥ punaḥPXY

yatra yogeśvaraḥ kṛṣṇoZKL
yatra pārtho dhanur-dharaḥZKL
tatra śrīr vijayo bhūtir dhruvā nītir matir mamaPXY


I want to merge the last two lines in each stanza.



I am trying in sublime text:



find: (?s)ZKLn((?!.*ZKL).*PXY)
replace: 1

(?s) for multi line search
ZKLn is ZKL and new line
((?!.*ZKL) to search from ZKL which is before PXY otherwse there are many ZKL
.*PXY everything till PXY


the above will only replace the last instance. How to see that all four instances in four stanzas are replaced.



i have lot off such stanzas










share|improve this question














I have the following text:



vyāsa-prasādāc chrutavānZKL
etad guhyam ahaṁ paramZKL
yogaṁ yogeśvarāt kṛṣṇātZKL
sākṣāt kathayataḥ svayamPXY

rājan saṁsmṛtya saṁsmṛtyaZKL
saṁvādam imam adbhutamZKL
keśavārjunayoḥ puṇyaṁZKL
hṛṣyāmi ca muhur muhuḥPXY

tac ca saṁsmṛtya saṁsmṛtyaZKL
rūpam aty-adbhutaṁ hareḥZKL
vismayo me mahān rājanZKL
hṛṣyāmi ca punaḥ punaḥPXY

yatra yogeśvaraḥ kṛṣṇoZKL
yatra pārtho dhanur-dharaḥZKL
tatra śrīr vijayo bhūtirZKL
dhruvā nītir matir mamaPXY


Now i want:



vyāsa-prasādāc chrutavānZKL
etad guhyam ahaṁ paramZKL
yogaṁ yogeśvarāt kṛṣṇāt sākṣāt kathayataḥ svayamPXY

rājan saṁsmṛtya saṁsmṛtyaZKL
saṁvādam imam adbhutamZKL
keśavārjunayoḥ puṇyaṁ hṛṣyāmi ca muhur muhuḥPXY

tac ca saṁsmṛtya saṁsmṛtyaZKL
rūpam aty-adbhutaṁ hareḥZKL
vismayo me mahān rājan hṛṣyāmi ca punaḥ punaḥPXY

yatra yogeśvaraḥ kṛṣṇoZKL
yatra pārtho dhanur-dharaḥZKL
tatra śrīr vijayo bhūtir dhruvā nītir matir mamaPXY


I want to merge the last two lines in each stanza.



I am trying in sublime text:



find: (?s)ZKLn((?!.*ZKL).*PXY)
replace: 1

(?s) for multi line search
ZKLn is ZKL and new line
((?!.*ZKL) to search from ZKL which is before PXY otherwse there are many ZKL
.*PXY everything till PXY


the above will only replace the last instance. How to see that all four instances in four stanzas are replaced.



i have lot off such stanzas







regex sublimetext3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 19:08









Santhosh YedidiSanthosh Yedidi

1,48832349




1,48832349













  • @ssc-hrep3 dictionary.com/browse/stanza

    – Sweeper
    Nov 24 '18 at 19:30



















  • @ssc-hrep3 dictionary.com/browse/stanza

    – Sweeper
    Nov 24 '18 at 19:30

















@ssc-hrep3 dictionary.com/browse/stanza

– Sweeper
Nov 24 '18 at 19:30





@ssc-hrep3 dictionary.com/browse/stanza

– Sweeper
Nov 24 '18 at 19:30












3 Answers
3






active

oldest

votes


















0














This regex seems to work PCRE. I am not sure about sublimetext3, can you try this regex?



ZKLn(?=.*nn)


and replace it with just a single space.



Note: I have inserted two new lines after the last paragraph, so that the regex works for last paragraph as well.



Check here



Edit:
In case you want to further combine PXY line with two lines above it, you can use ZKLn and replace it with space.



But in case you want to combine all four lines into one whist removing ZKL and PXY at the end, you can use this regex,



(ZKL|PXY(n))n


and remove it with a 22



Demo






share|improve this answer


























  • @Santhosh: I've updated my answer for the requirement you mentioned in a comment in another answer. Hope that helps. Let me know if you face any issue further or have any queries.

    – Pushpesh Kumar Rajwanshi
    Nov 24 '18 at 19:55



















1














You can try this:



ZKLn(?=.*PXYnn)


It matches ZKL followed by a new line, then checks if there are a bunch of non-new-line characters followed by PXY followed by 2 new lines after that.



You can replace it with a space.



Note that for this to work for the last stanza, you need to have at least two new lines at the end of the file.



Demo






share|improve this answer
























  • After joining the last two lines, Now i also want to join the line above PXY with the two lines above PXY

    – Santhosh Yedidi
    Nov 24 '18 at 19:28











  • @SanthoshYedidi Can you show what you expect to get for the first stanza?

    – Sweeper
    Nov 24 '18 at 19:30



















0














This regex should do the job:



(.+ZKLn.+ZKLn.+ZKL)n(.+PXY)


The regex matches any number of any char up to 'ZKLn'. This is repeated three times and is stored in Group 1.



Then it matches any number of any char up to 'PXY' This is stored in Group 2.



Now you replace with:



$1$2 or in some languages: 12



That will join the last two lines.



You should set the global flag.






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%2f53461508%2fregex-how-to-find-and-replace-at-multiple-instance-along-multi-line-search%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









    0














    This regex seems to work PCRE. I am not sure about sublimetext3, can you try this regex?



    ZKLn(?=.*nn)


    and replace it with just a single space.



    Note: I have inserted two new lines after the last paragraph, so that the regex works for last paragraph as well.



    Check here



    Edit:
    In case you want to further combine PXY line with two lines above it, you can use ZKLn and replace it with space.



    But in case you want to combine all four lines into one whist removing ZKL and PXY at the end, you can use this regex,



    (ZKL|PXY(n))n


    and remove it with a 22



    Demo






    share|improve this answer


























    • @Santhosh: I've updated my answer for the requirement you mentioned in a comment in another answer. Hope that helps. Let me know if you face any issue further or have any queries.

      – Pushpesh Kumar Rajwanshi
      Nov 24 '18 at 19:55
















    0














    This regex seems to work PCRE. I am not sure about sublimetext3, can you try this regex?



    ZKLn(?=.*nn)


    and replace it with just a single space.



    Note: I have inserted two new lines after the last paragraph, so that the regex works for last paragraph as well.



    Check here



    Edit:
    In case you want to further combine PXY line with two lines above it, you can use ZKLn and replace it with space.



    But in case you want to combine all four lines into one whist removing ZKL and PXY at the end, you can use this regex,



    (ZKL|PXY(n))n


    and remove it with a 22



    Demo






    share|improve this answer


























    • @Santhosh: I've updated my answer for the requirement you mentioned in a comment in another answer. Hope that helps. Let me know if you face any issue further or have any queries.

      – Pushpesh Kumar Rajwanshi
      Nov 24 '18 at 19:55














    0












    0








    0







    This regex seems to work PCRE. I am not sure about sublimetext3, can you try this regex?



    ZKLn(?=.*nn)


    and replace it with just a single space.



    Note: I have inserted two new lines after the last paragraph, so that the regex works for last paragraph as well.



    Check here



    Edit:
    In case you want to further combine PXY line with two lines above it, you can use ZKLn and replace it with space.



    But in case you want to combine all four lines into one whist removing ZKL and PXY at the end, you can use this regex,



    (ZKL|PXY(n))n


    and remove it with a 22



    Demo






    share|improve this answer















    This regex seems to work PCRE. I am not sure about sublimetext3, can you try this regex?



    ZKLn(?=.*nn)


    and replace it with just a single space.



    Note: I have inserted two new lines after the last paragraph, so that the regex works for last paragraph as well.



    Check here



    Edit:
    In case you want to further combine PXY line with two lines above it, you can use ZKLn and replace it with space.



    But in case you want to combine all four lines into one whist removing ZKL and PXY at the end, you can use this regex,



    (ZKL|PXY(n))n


    and remove it with a 22



    Demo







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 24 '18 at 20:03

























    answered Nov 24 '18 at 19:17









    Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

    9,08121028




    9,08121028













    • @Santhosh: I've updated my answer for the requirement you mentioned in a comment in another answer. Hope that helps. Let me know if you face any issue further or have any queries.

      – Pushpesh Kumar Rajwanshi
      Nov 24 '18 at 19:55



















    • @Santhosh: I've updated my answer for the requirement you mentioned in a comment in another answer. Hope that helps. Let me know if you face any issue further or have any queries.

      – Pushpesh Kumar Rajwanshi
      Nov 24 '18 at 19:55

















    @Santhosh: I've updated my answer for the requirement you mentioned in a comment in another answer. Hope that helps. Let me know if you face any issue further or have any queries.

    – Pushpesh Kumar Rajwanshi
    Nov 24 '18 at 19:55





    @Santhosh: I've updated my answer for the requirement you mentioned in a comment in another answer. Hope that helps. Let me know if you face any issue further or have any queries.

    – Pushpesh Kumar Rajwanshi
    Nov 24 '18 at 19:55













    1














    You can try this:



    ZKLn(?=.*PXYnn)


    It matches ZKL followed by a new line, then checks if there are a bunch of non-new-line characters followed by PXY followed by 2 new lines after that.



    You can replace it with a space.



    Note that for this to work for the last stanza, you need to have at least two new lines at the end of the file.



    Demo






    share|improve this answer
























    • After joining the last two lines, Now i also want to join the line above PXY with the two lines above PXY

      – Santhosh Yedidi
      Nov 24 '18 at 19:28











    • @SanthoshYedidi Can you show what you expect to get for the first stanza?

      – Sweeper
      Nov 24 '18 at 19:30
















    1














    You can try this:



    ZKLn(?=.*PXYnn)


    It matches ZKL followed by a new line, then checks if there are a bunch of non-new-line characters followed by PXY followed by 2 new lines after that.



    You can replace it with a space.



    Note that for this to work for the last stanza, you need to have at least two new lines at the end of the file.



    Demo






    share|improve this answer
























    • After joining the last two lines, Now i also want to join the line above PXY with the two lines above PXY

      – Santhosh Yedidi
      Nov 24 '18 at 19:28











    • @SanthoshYedidi Can you show what you expect to get for the first stanza?

      – Sweeper
      Nov 24 '18 at 19:30














    1












    1








    1







    You can try this:



    ZKLn(?=.*PXYnn)


    It matches ZKL followed by a new line, then checks if there are a bunch of non-new-line characters followed by PXY followed by 2 new lines after that.



    You can replace it with a space.



    Note that for this to work for the last stanza, you need to have at least two new lines at the end of the file.



    Demo






    share|improve this answer













    You can try this:



    ZKLn(?=.*PXYnn)


    It matches ZKL followed by a new line, then checks if there are a bunch of non-new-line characters followed by PXY followed by 2 new lines after that.



    You can replace it with a space.



    Note that for this to work for the last stanza, you need to have at least two new lines at the end of the file.



    Demo







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 19:22









    SweeperSweeper

    69.2k1074140




    69.2k1074140













    • After joining the last two lines, Now i also want to join the line above PXY with the two lines above PXY

      – Santhosh Yedidi
      Nov 24 '18 at 19:28











    • @SanthoshYedidi Can you show what you expect to get for the first stanza?

      – Sweeper
      Nov 24 '18 at 19:30



















    • After joining the last two lines, Now i also want to join the line above PXY with the two lines above PXY

      – Santhosh Yedidi
      Nov 24 '18 at 19:28











    • @SanthoshYedidi Can you show what you expect to get for the first stanza?

      – Sweeper
      Nov 24 '18 at 19:30

















    After joining the last two lines, Now i also want to join the line above PXY with the two lines above PXY

    – Santhosh Yedidi
    Nov 24 '18 at 19:28





    After joining the last two lines, Now i also want to join the line above PXY with the two lines above PXY

    – Santhosh Yedidi
    Nov 24 '18 at 19:28













    @SanthoshYedidi Can you show what you expect to get for the first stanza?

    – Sweeper
    Nov 24 '18 at 19:30





    @SanthoshYedidi Can you show what you expect to get for the first stanza?

    – Sweeper
    Nov 24 '18 at 19:30











    0














    This regex should do the job:



    (.+ZKLn.+ZKLn.+ZKL)n(.+PXY)


    The regex matches any number of any char up to 'ZKLn'. This is repeated three times and is stored in Group 1.



    Then it matches any number of any char up to 'PXY' This is stored in Group 2.



    Now you replace with:



    $1$2 or in some languages: 12



    That will join the last two lines.



    You should set the global flag.






    share|improve this answer




























      0














      This regex should do the job:



      (.+ZKLn.+ZKLn.+ZKL)n(.+PXY)


      The regex matches any number of any char up to 'ZKLn'. This is repeated three times and is stored in Group 1.



      Then it matches any number of any char up to 'PXY' This is stored in Group 2.



      Now you replace with:



      $1$2 or in some languages: 12



      That will join the last two lines.



      You should set the global flag.






      share|improve this answer


























        0












        0








        0







        This regex should do the job:



        (.+ZKLn.+ZKLn.+ZKL)n(.+PXY)


        The regex matches any number of any char up to 'ZKLn'. This is repeated three times and is stored in Group 1.



        Then it matches any number of any char up to 'PXY' This is stored in Group 2.



        Now you replace with:



        $1$2 or in some languages: 12



        That will join the last two lines.



        You should set the global flag.






        share|improve this answer













        This regex should do the job:



        (.+ZKLn.+ZKLn.+ZKL)n(.+PXY)


        The regex matches any number of any char up to 'ZKLn'. This is repeated three times and is stored in Group 1.



        Then it matches any number of any char up to 'PXY' This is stored in Group 2.



        Now you replace with:



        $1$2 or in some languages: 12



        That will join the last two lines.



        You should set the global flag.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 19:24









        Poul BakPoul Bak

        5,48831233




        5,48831233






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461508%2fregex-how-to-find-and-replace-at-multiple-instance-along-multi-line-search%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

            Ottavio Pratesi

            Tricia Helfer

            15 giugno