Condition string matches reg.expression

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












So, in my script, I have to decide whether one of the parameters is a valid email address.
I was trying, but it failed.



if $maddr="^.$*(@)(*)(.)(??*)"

then
...


It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.










share|improve this question



























    up vote
    2
    down vote

    favorite












    So, in my script, I have to decide whether one of the parameters is a valid email address.
    I was trying, but it failed.



    if $maddr="^.$*(@)(*)(.)(??*)"

    then
    ...


    It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      So, in my script, I have to decide whether one of the parameters is a valid email address.
      I was trying, but it failed.



      if $maddr="^.$*(@)(*)(.)(??*)"

      then
      ...


      It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.










      share|improve this question















      So, in my script, I have to decide whether one of the parameters is a valid email address.
      I was trying, but it failed.



      if $maddr="^.$*(@)(*)(.)(??*)"

      then
      ...


      It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.







      shell-script regular-expression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 27 mins ago









      Rui F Ribeiro

      37.3k1374118




      37.3k1374118










      asked Apr 28 '14 at 21:51









      Wanderer

      1842311




      1842311




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          The Unix tool to match a string against a regexp is expr:



          if expr "$maddr" : '..*@.*...' > /dev/null; then...


          (note that expr regexps are implicitly anchored at the beginning)



          Though in this case, simple shell pattern matching would be enough:



          case $maddr in
          ?*@*.??*) ...
          esac


          Note that some shells like zsh, ksh93 and bash have builtin regexp matching operators as an extension above the standard sh syntax, but the syntax varies slightly across those.



          pattern='.@.*...'
          if [[ $maddr =~ $pattern ]]; then...


          Should work across all three.



          (note that those patterns don't guarantee a valid email address).






          share|improve this answer




















          • second one somehow doesn't work, but first one did. thanks for your time.
            – Wanderer
            Apr 29 '14 at 13:01

















          up vote
          1
          down vote













          _vaddr() cat
          printf %s\n "$@"






          share|improve this answer






















          • grep tries to match every line of $maddr, not $maddr as a whole. Also note that echo does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep you could do printf %s "$maddr" | grep -zq "$regex"
            – Stéphane Chazelas
            Apr 29 '14 at 6:36







          • 1




            @StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to printf. grep -z i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
            – mikeserv
            Apr 29 '14 at 6:52











          • grep -q on non-text input is not portable either, you need printf '%sn'. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
            – Stéphane Chazelas
            Apr 29 '14 at 6:54










          • @StephaneChezales The "$REGEX" is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
            – mikeserv
            Apr 29 '14 at 7:00










          • What I meant is if $maddr may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
            – Stéphane Chazelas
            Apr 29 '14 at 7:09










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f126989%2fcondition-string-matches-reg-expression%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          The Unix tool to match a string against a regexp is expr:



          if expr "$maddr" : '..*@.*...' > /dev/null; then...


          (note that expr regexps are implicitly anchored at the beginning)



          Though in this case, simple shell pattern matching would be enough:



          case $maddr in
          ?*@*.??*) ...
          esac


          Note that some shells like zsh, ksh93 and bash have builtin regexp matching operators as an extension above the standard sh syntax, but the syntax varies slightly across those.



          pattern='.@.*...'
          if [[ $maddr =~ $pattern ]]; then...


          Should work across all three.



          (note that those patterns don't guarantee a valid email address).






          share|improve this answer




















          • second one somehow doesn't work, but first one did. thanks for your time.
            – Wanderer
            Apr 29 '14 at 13:01














          up vote
          4
          down vote



          accepted










          The Unix tool to match a string against a regexp is expr:



          if expr "$maddr" : '..*@.*...' > /dev/null; then...


          (note that expr regexps are implicitly anchored at the beginning)



          Though in this case, simple shell pattern matching would be enough:



          case $maddr in
          ?*@*.??*) ...
          esac


          Note that some shells like zsh, ksh93 and bash have builtin regexp matching operators as an extension above the standard sh syntax, but the syntax varies slightly across those.



          pattern='.@.*...'
          if [[ $maddr =~ $pattern ]]; then...


          Should work across all three.



          (note that those patterns don't guarantee a valid email address).






          share|improve this answer




















          • second one somehow doesn't work, but first one did. thanks for your time.
            – Wanderer
            Apr 29 '14 at 13:01












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          The Unix tool to match a string against a regexp is expr:



          if expr "$maddr" : '..*@.*...' > /dev/null; then...


          (note that expr regexps are implicitly anchored at the beginning)



          Though in this case, simple shell pattern matching would be enough:



          case $maddr in
          ?*@*.??*) ...
          esac


          Note that some shells like zsh, ksh93 and bash have builtin regexp matching operators as an extension above the standard sh syntax, but the syntax varies slightly across those.



          pattern='.@.*...'
          if [[ $maddr =~ $pattern ]]; then...


          Should work across all three.



          (note that those patterns don't guarantee a valid email address).






          share|improve this answer












          The Unix tool to match a string against a regexp is expr:



          if expr "$maddr" : '..*@.*...' > /dev/null; then...


          (note that expr regexps are implicitly anchored at the beginning)



          Though in this case, simple shell pattern matching would be enough:



          case $maddr in
          ?*@*.??*) ...
          esac


          Note that some shells like zsh, ksh93 and bash have builtin regexp matching operators as an extension above the standard sh syntax, but the syntax varies slightly across those.



          pattern='.@.*...'
          if [[ $maddr =~ $pattern ]]; then...


          Should work across all three.



          (note that those patterns don't guarantee a valid email address).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 28 '14 at 22:00









          Stéphane Chazelas

          289k54536874




          289k54536874











          • second one somehow doesn't work, but first one did. thanks for your time.
            – Wanderer
            Apr 29 '14 at 13:01
















          • second one somehow doesn't work, but first one did. thanks for your time.
            – Wanderer
            Apr 29 '14 at 13:01















          second one somehow doesn't work, but first one did. thanks for your time.
          – Wanderer
          Apr 29 '14 at 13:01




          second one somehow doesn't work, but first one did. thanks for your time.
          – Wanderer
          Apr 29 '14 at 13:01












          up vote
          1
          down vote













          _vaddr() cat
          printf %s\n "$@"






          share|improve this answer






















          • grep tries to match every line of $maddr, not $maddr as a whole. Also note that echo does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep you could do printf %s "$maddr" | grep -zq "$regex"
            – Stéphane Chazelas
            Apr 29 '14 at 6:36







          • 1




            @StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to printf. grep -z i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
            – mikeserv
            Apr 29 '14 at 6:52











          • grep -q on non-text input is not portable either, you need printf '%sn'. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
            – Stéphane Chazelas
            Apr 29 '14 at 6:54










          • @StephaneChezales The "$REGEX" is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
            – mikeserv
            Apr 29 '14 at 7:00










          • What I meant is if $maddr may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
            – Stéphane Chazelas
            Apr 29 '14 at 7:09














          up vote
          1
          down vote













          _vaddr() cat
          printf %s\n "$@"






          share|improve this answer






















          • grep tries to match every line of $maddr, not $maddr as a whole. Also note that echo does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep you could do printf %s "$maddr" | grep -zq "$regex"
            – Stéphane Chazelas
            Apr 29 '14 at 6:36







          • 1




            @StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to printf. grep -z i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
            – mikeserv
            Apr 29 '14 at 6:52











          • grep -q on non-text input is not portable either, you need printf '%sn'. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
            – Stéphane Chazelas
            Apr 29 '14 at 6:54










          • @StephaneChezales The "$REGEX" is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
            – mikeserv
            Apr 29 '14 at 7:00










          • What I meant is if $maddr may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
            – Stéphane Chazelas
            Apr 29 '14 at 7:09












          up vote
          1
          down vote










          up vote
          1
          down vote









          _vaddr() cat
          printf %s\n "$@"






          share|improve this answer














          _vaddr() cat
          printf %s\n "$@"







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 29 '14 at 7:12

























          answered Apr 29 '14 at 0:11









          mikeserv

          44.7k565151




          44.7k565151











          • grep tries to match every line of $maddr, not $maddr as a whole. Also note that echo does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep you could do printf %s "$maddr" | grep -zq "$regex"
            – Stéphane Chazelas
            Apr 29 '14 at 6:36







          • 1




            @StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to printf. grep -z i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
            – mikeserv
            Apr 29 '14 at 6:52











          • grep -q on non-text input is not portable either, you need printf '%sn'. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
            – Stéphane Chazelas
            Apr 29 '14 at 6:54










          • @StephaneChezales The "$REGEX" is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
            – mikeserv
            Apr 29 '14 at 7:00










          • What I meant is if $maddr may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
            – Stéphane Chazelas
            Apr 29 '14 at 7:09
















          • grep tries to match every line of $maddr, not $maddr as a whole. Also note that echo does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep you could do printf %s "$maddr" | grep -zq "$regex"
            – Stéphane Chazelas
            Apr 29 '14 at 6:36







          • 1




            @StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to printf. grep -z i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
            – mikeserv
            Apr 29 '14 at 6:52











          • grep -q on non-text input is not portable either, you need printf '%sn'. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
            – Stéphane Chazelas
            Apr 29 '14 at 6:54










          • @StephaneChezales The "$REGEX" is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
            – mikeserv
            Apr 29 '14 at 7:00










          • What I meant is if $maddr may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
            – Stéphane Chazelas
            Apr 29 '14 at 7:09















          grep tries to match every line of $maddr, not $maddr as a whole. Also note that echo does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep you could do printf %s "$maddr" | grep -zq "$regex"
          – Stéphane Chazelas
          Apr 29 '14 at 6:36





          grep tries to match every line of $maddr, not $maddr as a whole. Also note that echo does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep you could do printf %s "$maddr" | grep -zq "$regex"
          – Stéphane Chazelas
          Apr 29 '14 at 6:36





          1




          1




          @StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to printf. grep -z i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
          – mikeserv
          Apr 29 '14 at 6:52





          @StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to printf. grep -z i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there.
          – mikeserv
          Apr 29 '14 at 6:52













          grep -q on non-text input is not portable either, you need printf '%sn'. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
          – Stéphane Chazelas
          Apr 29 '14 at 6:54




          grep -q on non-text input is not portable either, you need printf '%sn'. It's about validating input so you can exclude things that are not email addresses (where anything could occur)
          – Stéphane Chazelas
          Apr 29 '14 at 6:54












          @StephaneChezales The "$REGEX" is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
          – mikeserv
          Apr 29 '14 at 7:00




          @StephaneChezales The "$REGEX" is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion.
          – mikeserv
          Apr 29 '14 at 7:00












          What I meant is if $maddr may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
          – Stéphane Chazelas
          Apr 29 '14 at 7:09




          What I meant is if $maddr may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches.
          – Stéphane Chazelas
          Apr 29 '14 at 7:09

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f126989%2fcondition-string-matches-reg-expression%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?