Regular expression for matching all variables in a shell script











up vote
1
down vote

favorite












Let's assume I have the following string:



cd $MY_DIR
This is some text:$MY_VARIABLE
Add some other text $1 $2 $FOO value=$BAR


What would be a valid regex to find all occurences of the variables?



I have tried the following expresion but it has some drawbacks as it:




  • also selects the blank

  • doesn't select $1 $2 individually


$.*










share|improve this question
























  • What tool are you using?
    – Wiktor Stribiżew
    Nov 19 at 11:54










  • I'm using VS Code. Added the tag to my question.
    – Robert Strauch
    Nov 19 at 11:54








  • 2




    Try B$w+....
    – Wiktor Stribiżew
    Nov 19 at 11:55










  • That would match $var which aren't variable reference though. Maybe with an additional lookbehind, e.g. B(?<!\)$w+ ?
    – Aaron
    Nov 19 at 11:58






  • 1




    @Aaron The lookbehind won't solve that anyway. One would need (?<!\)(?:\{2})*$[a-zA-Z_]w*.
    – Wiktor Stribiżew
    Nov 19 at 11:58

















up vote
1
down vote

favorite












Let's assume I have the following string:



cd $MY_DIR
This is some text:$MY_VARIABLE
Add some other text $1 $2 $FOO value=$BAR


What would be a valid regex to find all occurences of the variables?



I have tried the following expresion but it has some drawbacks as it:




  • also selects the blank

  • doesn't select $1 $2 individually


$.*










share|improve this question
























  • What tool are you using?
    – Wiktor Stribiżew
    Nov 19 at 11:54










  • I'm using VS Code. Added the tag to my question.
    – Robert Strauch
    Nov 19 at 11:54








  • 2




    Try B$w+....
    – Wiktor Stribiżew
    Nov 19 at 11:55










  • That would match $var which aren't variable reference though. Maybe with an additional lookbehind, e.g. B(?<!\)$w+ ?
    – Aaron
    Nov 19 at 11:58






  • 1




    @Aaron The lookbehind won't solve that anyway. One would need (?<!\)(?:\{2})*$[a-zA-Z_]w*.
    – Wiktor Stribiżew
    Nov 19 at 11:58















up vote
1
down vote

favorite









up vote
1
down vote

favorite











Let's assume I have the following string:



cd $MY_DIR
This is some text:$MY_VARIABLE
Add some other text $1 $2 $FOO value=$BAR


What would be a valid regex to find all occurences of the variables?



I have tried the following expresion but it has some drawbacks as it:




  • also selects the blank

  • doesn't select $1 $2 individually


$.*










share|improve this question















Let's assume I have the following string:



cd $MY_DIR
This is some text:$MY_VARIABLE
Add some other text $1 $2 $FOO value=$BAR


What would be a valid regex to find all occurences of the variables?



I have tried the following expresion but it has some drawbacks as it:




  • also selects the blank

  • doesn't select $1 $2 individually


$.*







regex visual-studio-code






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 11:55

























asked Nov 19 at 11:53









Robert Strauch

3,643105599




3,643105599












  • What tool are you using?
    – Wiktor Stribiżew
    Nov 19 at 11:54










  • I'm using VS Code. Added the tag to my question.
    – Robert Strauch
    Nov 19 at 11:54








  • 2




    Try B$w+....
    – Wiktor Stribiżew
    Nov 19 at 11:55










  • That would match $var which aren't variable reference though. Maybe with an additional lookbehind, e.g. B(?<!\)$w+ ?
    – Aaron
    Nov 19 at 11:58






  • 1




    @Aaron The lookbehind won't solve that anyway. One would need (?<!\)(?:\{2})*$[a-zA-Z_]w*.
    – Wiktor Stribiżew
    Nov 19 at 11:58




















  • What tool are you using?
    – Wiktor Stribiżew
    Nov 19 at 11:54










  • I'm using VS Code. Added the tag to my question.
    – Robert Strauch
    Nov 19 at 11:54








  • 2




    Try B$w+....
    – Wiktor Stribiżew
    Nov 19 at 11:55










  • That would match $var which aren't variable reference though. Maybe with an additional lookbehind, e.g. B(?<!\)$w+ ?
    – Aaron
    Nov 19 at 11:58






  • 1




    @Aaron The lookbehind won't solve that anyway. One would need (?<!\)(?:\{2})*$[a-zA-Z_]w*.
    – Wiktor Stribiżew
    Nov 19 at 11:58


















What tool are you using?
– Wiktor Stribiżew
Nov 19 at 11:54




What tool are you using?
– Wiktor Stribiżew
Nov 19 at 11:54












I'm using VS Code. Added the tag to my question.
– Robert Strauch
Nov 19 at 11:54






I'm using VS Code. Added the tag to my question.
– Robert Strauch
Nov 19 at 11:54






2




2




Try B$w+....
– Wiktor Stribiżew
Nov 19 at 11:55




Try B$w+....
– Wiktor Stribiżew
Nov 19 at 11:55












That would match $var which aren't variable reference though. Maybe with an additional lookbehind, e.g. B(?<!\)$w+ ?
– Aaron
Nov 19 at 11:58




That would match $var which aren't variable reference though. Maybe with an additional lookbehind, e.g. B(?<!\)$w+ ?
– Aaron
Nov 19 at 11:58




1




1




@Aaron The lookbehind won't solve that anyway. One would need (?<!\)(?:\{2})*$[a-zA-Z_]w*.
– Wiktor Stribiżew
Nov 19 at 11:58






@Aaron The lookbehind won't solve that anyway. One would need (?<!\)(?:\{2})*$[a-zA-Z_]w*.
– Wiktor Stribiżew
Nov 19 at 11:58














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You may use



B$w+


that matches a $ that is not preceded with a letter, digit or _ and then 1 or more letters, digits or _.



enter image description here






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%2f53374088%2fregular-expression-for-matching-all-variables-in-a-shell-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You may use



    B$w+


    that matches a $ that is not preceded with a letter, digit or _ and then 1 or more letters, digits or _.



    enter image description here






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      You may use



      B$w+


      that matches a $ that is not preceded with a letter, digit or _ and then 1 or more letters, digits or _.



      enter image description here






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You may use



        B$w+


        that matches a $ that is not preceded with a letter, digit or _ and then 1 or more letters, digits or _.



        enter image description here






        share|improve this answer












        You may use



        B$w+


        that matches a $ that is not preceded with a letter, digit or _ and then 1 or more letters, digits or _.



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 12:06









        Wiktor Stribiżew

        301k16122197




        301k16122197






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374088%2fregular-expression-for-matching-all-variables-in-a-shell-script%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

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen