Using regex to select AFTER one line all the way to a specific word on another line












0















I'm not sure if this is possible. I have a list with specific words on new lines, and I need to select the words between those lines. For example my source is:



Word_1
Word_2
Location
Variable_1
Variable_2
Variable_3
Section
Word_9


I need regex to find the line after Location, which I am doing using (.*(?<=bLocations)(w+).*)|.* and replacing with $1. However this only gives me Variable_1 and I need it to give me Variable_1,Variable_2,Variable_3. And here's the catch, sometimes there is one Variable, sometimes 2, sometimes 3, sometimes 4. BUT, the following word will always be Section. So I'm thinking I need basically some way to tell Regex to select every line after Location but before Section.



Any ideas?



Real world example:



Category
Business
Dates
StatusOpen
Closing Information
Location
National
South-East Asia
New South Wales
Victoria
Sections
General
Difficulty Rating
Administrator


Output would be National,South-East Asia,New South Wales,Victoria










share|improve this question

























  • What language or app are you working in?

    – Andy Lester
    Nov 20 '18 at 1:12


















0















I'm not sure if this is possible. I have a list with specific words on new lines, and I need to select the words between those lines. For example my source is:



Word_1
Word_2
Location
Variable_1
Variable_2
Variable_3
Section
Word_9


I need regex to find the line after Location, which I am doing using (.*(?<=bLocations)(w+).*)|.* and replacing with $1. However this only gives me Variable_1 and I need it to give me Variable_1,Variable_2,Variable_3. And here's the catch, sometimes there is one Variable, sometimes 2, sometimes 3, sometimes 4. BUT, the following word will always be Section. So I'm thinking I need basically some way to tell Regex to select every line after Location but before Section.



Any ideas?



Real world example:



Category
Business
Dates
StatusOpen
Closing Information
Location
National
South-East Asia
New South Wales
Victoria
Sections
General
Difficulty Rating
Administrator


Output would be National,South-East Asia,New South Wales,Victoria










share|improve this question

























  • What language or app are you working in?

    – Andy Lester
    Nov 20 '18 at 1:12
















0












0








0








I'm not sure if this is possible. I have a list with specific words on new lines, and I need to select the words between those lines. For example my source is:



Word_1
Word_2
Location
Variable_1
Variable_2
Variable_3
Section
Word_9


I need regex to find the line after Location, which I am doing using (.*(?<=bLocations)(w+).*)|.* and replacing with $1. However this only gives me Variable_1 and I need it to give me Variable_1,Variable_2,Variable_3. And here's the catch, sometimes there is one Variable, sometimes 2, sometimes 3, sometimes 4. BUT, the following word will always be Section. So I'm thinking I need basically some way to tell Regex to select every line after Location but before Section.



Any ideas?



Real world example:



Category
Business
Dates
StatusOpen
Closing Information
Location
National
South-East Asia
New South Wales
Victoria
Sections
General
Difficulty Rating
Administrator


Output would be National,South-East Asia,New South Wales,Victoria










share|improve this question
















I'm not sure if this is possible. I have a list with specific words on new lines, and I need to select the words between those lines. For example my source is:



Word_1
Word_2
Location
Variable_1
Variable_2
Variable_3
Section
Word_9


I need regex to find the line after Location, which I am doing using (.*(?<=bLocations)(w+).*)|.* and replacing with $1. However this only gives me Variable_1 and I need it to give me Variable_1,Variable_2,Variable_3. And here's the catch, sometimes there is one Variable, sometimes 2, sometimes 3, sometimes 4. BUT, the following word will always be Section. So I'm thinking I need basically some way to tell Regex to select every line after Location but before Section.



Any ideas?



Real world example:



Category
Business
Dates
StatusOpen
Closing Information
Location
National
South-East Asia
New South Wales
Victoria
Sections
General
Difficulty Rating
Administrator


Output would be National,South-East Asia,New South Wales,Victoria







php regex wordpress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 1:35







Mike

















asked Nov 20 '18 at 0:31









MikeMike

12




12













  • What language or app are you working in?

    – Andy Lester
    Nov 20 '18 at 1:12





















  • What language or app are you working in?

    – Andy Lester
    Nov 20 '18 at 1:12



















What language or app are you working in?

– Andy Lester
Nov 20 '18 at 1:12







What language or app are you working in?

– Andy Lester
Nov 20 '18 at 1:12














2 Answers
2






active

oldest

votes


















0














In Python you can use DOTALL in your re.



print(re.findall("Location(.*)Section",string,re.DOTALL)[0])


Output:



Variable_1
Variable_2
Variable_3


For PHP can you try the below.



'w*Location(.*)Section/s'


You can check the output in this link for your example.



Regex101 link



Output match:



National
South-East Asia
New South Wales
Victoria





share|improve this answer


























  • Can't use Python unfortunately. Thanks though.

    – Mike
    Nov 20 '18 at 0:51






  • 1





    which language are you using it?

    – Ashok KS
    Nov 20 '18 at 1:01











  • It's in a wordpress plugin I am assuming it's just general notepad++ type find and replace.

    – Mike
    Nov 20 '18 at 1:13






  • 1





    wordpress is php based. So you should use php regex functions fine.

    – Edilson Borges
    Nov 20 '18 at 1:15











  • Lemme clear your question, you need to select the words that don't have any _, right? Such as Location and Section

    – Edilson Borges
    Nov 20 '18 at 1:17





















0














A solution with PHP would be:



<?php

$string =
"Category
Business
Dates
StatusOpen
Closing Information
Location
National
South-East Asia
New South Wales
Victoria
Sections
General
Difficulty Rating
Administrator";

$initialValue = false;
$lastValue = false;
$arResult = ;
$arValue = explode("n", $string);

foreach($arValue as $value) {
$value = trim($value);
if ($value == "Location") {
$initialValue = true;
} else if ($value == "Sections") {
$lastValue = true;
} else if ($initialValue == true && $lastValue == false) {
$arResult = $value;
}
}

echo implode(",",$arResult); // National,South-East Asia,New South Wales,Victoria





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%2f53384570%2fusing-regex-to-select-after-one-line-all-the-way-to-a-specific-word-on-another-l%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









    0














    In Python you can use DOTALL in your re.



    print(re.findall("Location(.*)Section",string,re.DOTALL)[0])


    Output:



    Variable_1
    Variable_2
    Variable_3


    For PHP can you try the below.



    'w*Location(.*)Section/s'


    You can check the output in this link for your example.



    Regex101 link



    Output match:



    National
    South-East Asia
    New South Wales
    Victoria





    share|improve this answer


























    • Can't use Python unfortunately. Thanks though.

      – Mike
      Nov 20 '18 at 0:51






    • 1





      which language are you using it?

      – Ashok KS
      Nov 20 '18 at 1:01











    • It's in a wordpress plugin I am assuming it's just general notepad++ type find and replace.

      – Mike
      Nov 20 '18 at 1:13






    • 1





      wordpress is php based. So you should use php regex functions fine.

      – Edilson Borges
      Nov 20 '18 at 1:15











    • Lemme clear your question, you need to select the words that don't have any _, right? Such as Location and Section

      – Edilson Borges
      Nov 20 '18 at 1:17


















    0














    In Python you can use DOTALL in your re.



    print(re.findall("Location(.*)Section",string,re.DOTALL)[0])


    Output:



    Variable_1
    Variable_2
    Variable_3


    For PHP can you try the below.



    'w*Location(.*)Section/s'


    You can check the output in this link for your example.



    Regex101 link



    Output match:



    National
    South-East Asia
    New South Wales
    Victoria





    share|improve this answer


























    • Can't use Python unfortunately. Thanks though.

      – Mike
      Nov 20 '18 at 0:51






    • 1





      which language are you using it?

      – Ashok KS
      Nov 20 '18 at 1:01











    • It's in a wordpress plugin I am assuming it's just general notepad++ type find and replace.

      – Mike
      Nov 20 '18 at 1:13






    • 1





      wordpress is php based. So you should use php regex functions fine.

      – Edilson Borges
      Nov 20 '18 at 1:15











    • Lemme clear your question, you need to select the words that don't have any _, right? Such as Location and Section

      – Edilson Borges
      Nov 20 '18 at 1:17
















    0












    0








    0







    In Python you can use DOTALL in your re.



    print(re.findall("Location(.*)Section",string,re.DOTALL)[0])


    Output:



    Variable_1
    Variable_2
    Variable_3


    For PHP can you try the below.



    'w*Location(.*)Section/s'


    You can check the output in this link for your example.



    Regex101 link



    Output match:



    National
    South-East Asia
    New South Wales
    Victoria





    share|improve this answer















    In Python you can use DOTALL in your re.



    print(re.findall("Location(.*)Section",string,re.DOTALL)[0])


    Output:



    Variable_1
    Variable_2
    Variable_3


    For PHP can you try the below.



    'w*Location(.*)Section/s'


    You can check the output in this link for your example.



    Regex101 link



    Output match:



    National
    South-East Asia
    New South Wales
    Victoria






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 1:38

























    answered Nov 20 '18 at 0:40









    Ashok KSAshok KS

    191214




    191214













    • Can't use Python unfortunately. Thanks though.

      – Mike
      Nov 20 '18 at 0:51






    • 1





      which language are you using it?

      – Ashok KS
      Nov 20 '18 at 1:01











    • It's in a wordpress plugin I am assuming it's just general notepad++ type find and replace.

      – Mike
      Nov 20 '18 at 1:13






    • 1





      wordpress is php based. So you should use php regex functions fine.

      – Edilson Borges
      Nov 20 '18 at 1:15











    • Lemme clear your question, you need to select the words that don't have any _, right? Such as Location and Section

      – Edilson Borges
      Nov 20 '18 at 1:17





















    • Can't use Python unfortunately. Thanks though.

      – Mike
      Nov 20 '18 at 0:51






    • 1





      which language are you using it?

      – Ashok KS
      Nov 20 '18 at 1:01











    • It's in a wordpress plugin I am assuming it's just general notepad++ type find and replace.

      – Mike
      Nov 20 '18 at 1:13






    • 1





      wordpress is php based. So you should use php regex functions fine.

      – Edilson Borges
      Nov 20 '18 at 1:15











    • Lemme clear your question, you need to select the words that don't have any _, right? Such as Location and Section

      – Edilson Borges
      Nov 20 '18 at 1:17



















    Can't use Python unfortunately. Thanks though.

    – Mike
    Nov 20 '18 at 0:51





    Can't use Python unfortunately. Thanks though.

    – Mike
    Nov 20 '18 at 0:51




    1




    1





    which language are you using it?

    – Ashok KS
    Nov 20 '18 at 1:01





    which language are you using it?

    – Ashok KS
    Nov 20 '18 at 1:01













    It's in a wordpress plugin I am assuming it's just general notepad++ type find and replace.

    – Mike
    Nov 20 '18 at 1:13





    It's in a wordpress plugin I am assuming it's just general notepad++ type find and replace.

    – Mike
    Nov 20 '18 at 1:13




    1




    1





    wordpress is php based. So you should use php regex functions fine.

    – Edilson Borges
    Nov 20 '18 at 1:15





    wordpress is php based. So you should use php regex functions fine.

    – Edilson Borges
    Nov 20 '18 at 1:15













    Lemme clear your question, you need to select the words that don't have any _, right? Such as Location and Section

    – Edilson Borges
    Nov 20 '18 at 1:17







    Lemme clear your question, you need to select the words that don't have any _, right? Such as Location and Section

    – Edilson Borges
    Nov 20 '18 at 1:17















    0














    A solution with PHP would be:



    <?php

    $string =
    "Category
    Business
    Dates
    StatusOpen
    Closing Information
    Location
    National
    South-East Asia
    New South Wales
    Victoria
    Sections
    General
    Difficulty Rating
    Administrator";

    $initialValue = false;
    $lastValue = false;
    $arResult = ;
    $arValue = explode("n", $string);

    foreach($arValue as $value) {
    $value = trim($value);
    if ($value == "Location") {
    $initialValue = true;
    } else if ($value == "Sections") {
    $lastValue = true;
    } else if ($initialValue == true && $lastValue == false) {
    $arResult = $value;
    }
    }

    echo implode(",",$arResult); // National,South-East Asia,New South Wales,Victoria





    share|improve this answer




























      0














      A solution with PHP would be:



      <?php

      $string =
      "Category
      Business
      Dates
      StatusOpen
      Closing Information
      Location
      National
      South-East Asia
      New South Wales
      Victoria
      Sections
      General
      Difficulty Rating
      Administrator";

      $initialValue = false;
      $lastValue = false;
      $arResult = ;
      $arValue = explode("n", $string);

      foreach($arValue as $value) {
      $value = trim($value);
      if ($value == "Location") {
      $initialValue = true;
      } else if ($value == "Sections") {
      $lastValue = true;
      } else if ($initialValue == true && $lastValue == false) {
      $arResult = $value;
      }
      }

      echo implode(",",$arResult); // National,South-East Asia,New South Wales,Victoria





      share|improve this answer


























        0












        0








        0







        A solution with PHP would be:



        <?php

        $string =
        "Category
        Business
        Dates
        StatusOpen
        Closing Information
        Location
        National
        South-East Asia
        New South Wales
        Victoria
        Sections
        General
        Difficulty Rating
        Administrator";

        $initialValue = false;
        $lastValue = false;
        $arResult = ;
        $arValue = explode("n", $string);

        foreach($arValue as $value) {
        $value = trim($value);
        if ($value == "Location") {
        $initialValue = true;
        } else if ($value == "Sections") {
        $lastValue = true;
        } else if ($initialValue == true && $lastValue == false) {
        $arResult = $value;
        }
        }

        echo implode(",",$arResult); // National,South-East Asia,New South Wales,Victoria





        share|improve this answer













        A solution with PHP would be:



        <?php

        $string =
        "Category
        Business
        Dates
        StatusOpen
        Closing Information
        Location
        National
        South-East Asia
        New South Wales
        Victoria
        Sections
        General
        Difficulty Rating
        Administrator";

        $initialValue = false;
        $lastValue = false;
        $arResult = ;
        $arValue = explode("n", $string);

        foreach($arValue as $value) {
        $value = trim($value);
        if ($value == "Location") {
        $initialValue = true;
        } else if ($value == "Sections") {
        $lastValue = true;
        } else if ($initialValue == true && $lastValue == false) {
        $arResult = $value;
        }
        }

        echo implode(",",$arResult); // National,South-East Asia,New South Wales,Victoria






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 2:16









        Edilson BorgesEdilson Borges

        333110




        333110






























            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%2f53384570%2fusing-regex-to-select-after-one-line-all-the-way-to-a-specific-word-on-another-l%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 change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?