How do I make a perl regular expresion fail in the code block?











up vote
1
down vote

favorite












I've got a screw I'm trying to nail in with perl and so far this is what I've got so far.



perl -ne '/(.+).(.+)((.+))(.+)(?{print "match" if  ( $1 > 9 || ( $1 == 9 && $2 > 1 ) || ($1 == 9 && $2 == 1 && $3 > 7 ) || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22 ) })/' versions


versions:



9.1(7)23
9.1(7)22
8.1(7)22
7.2(33)24
55


it will print "match" if the version in the file is > than 9.1(7)23, which is good.



But I want the regexp to succeed, not just print "match". How do I translate the stuff inside the code block to an actual response. I've tried quite a few iterations with *ACCEPT|*FAIL but nothing has worked so far.










share|improve this question






















  • Maybe using (??{}) instead of (?{}): /(.+).(.+)((.+))(.+)(??{ your_condition ? "" : "(*FAIL)"})/ ? I mean, this is OK if you are writing some quick one-liner for a one time job; but if this is supposed to be maintainable, production code, then don't try to do this with just a regex of course ;)
    – Dada
    2 days ago












  • (peharps "(*ACCEPT)" instead of "". Both work, and I have no opinion on which is "better". ACCEPT is more explicit I guess; but since you are writing something not readable at all, does it really matter?)
    – Dada
    2 days ago










  • @dada yeah, it's gotta be a one liner. I'm injecting into some version matching thing into some script to help a network guy out using their own tools. But that totally worked, thanks a lot you want to make it a legit answer to get credit?
    – Peter Turner
    2 days ago

















up vote
1
down vote

favorite












I've got a screw I'm trying to nail in with perl and so far this is what I've got so far.



perl -ne '/(.+).(.+)((.+))(.+)(?{print "match" if  ( $1 > 9 || ( $1 == 9 && $2 > 1 ) || ($1 == 9 && $2 == 1 && $3 > 7 ) || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22 ) })/' versions


versions:



9.1(7)23
9.1(7)22
8.1(7)22
7.2(33)24
55


it will print "match" if the version in the file is > than 9.1(7)23, which is good.



But I want the regexp to succeed, not just print "match". How do I translate the stuff inside the code block to an actual response. I've tried quite a few iterations with *ACCEPT|*FAIL but nothing has worked so far.










share|improve this question






















  • Maybe using (??{}) instead of (?{}): /(.+).(.+)((.+))(.+)(??{ your_condition ? "" : "(*FAIL)"})/ ? I mean, this is OK if you are writing some quick one-liner for a one time job; but if this is supposed to be maintainable, production code, then don't try to do this with just a regex of course ;)
    – Dada
    2 days ago












  • (peharps "(*ACCEPT)" instead of "". Both work, and I have no opinion on which is "better". ACCEPT is more explicit I guess; but since you are writing something not readable at all, does it really matter?)
    – Dada
    2 days ago










  • @dada yeah, it's gotta be a one liner. I'm injecting into some version matching thing into some script to help a network guy out using their own tools. But that totally worked, thanks a lot you want to make it a legit answer to get credit?
    – Peter Turner
    2 days ago















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've got a screw I'm trying to nail in with perl and so far this is what I've got so far.



perl -ne '/(.+).(.+)((.+))(.+)(?{print "match" if  ( $1 > 9 || ( $1 == 9 && $2 > 1 ) || ($1 == 9 && $2 == 1 && $3 > 7 ) || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22 ) })/' versions


versions:



9.1(7)23
9.1(7)22
8.1(7)22
7.2(33)24
55


it will print "match" if the version in the file is > than 9.1(7)23, which is good.



But I want the regexp to succeed, not just print "match". How do I translate the stuff inside the code block to an actual response. I've tried quite a few iterations with *ACCEPT|*FAIL but nothing has worked so far.










share|improve this question













I've got a screw I'm trying to nail in with perl and so far this is what I've got so far.



perl -ne '/(.+).(.+)((.+))(.+)(?{print "match" if  ( $1 > 9 || ( $1 == 9 && $2 > 1 ) || ($1 == 9 && $2 == 1 && $3 > 7 ) || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22 ) })/' versions


versions:



9.1(7)23
9.1(7)22
8.1(7)22
7.2(33)24
55


it will print "match" if the version in the file is > than 9.1(7)23, which is good.



But I want the regexp to succeed, not just print "match". How do I translate the stuff inside the code block to an actual response. I've tried quite a few iterations with *ACCEPT|*FAIL but nothing has worked so far.







perl pcre






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Peter Turner

4,891753101




4,891753101












  • Maybe using (??{}) instead of (?{}): /(.+).(.+)((.+))(.+)(??{ your_condition ? "" : "(*FAIL)"})/ ? I mean, this is OK if you are writing some quick one-liner for a one time job; but if this is supposed to be maintainable, production code, then don't try to do this with just a regex of course ;)
    – Dada
    2 days ago












  • (peharps "(*ACCEPT)" instead of "". Both work, and I have no opinion on which is "better". ACCEPT is more explicit I guess; but since you are writing something not readable at all, does it really matter?)
    – Dada
    2 days ago










  • @dada yeah, it's gotta be a one liner. I'm injecting into some version matching thing into some script to help a network guy out using their own tools. But that totally worked, thanks a lot you want to make it a legit answer to get credit?
    – Peter Turner
    2 days ago




















  • Maybe using (??{}) instead of (?{}): /(.+).(.+)((.+))(.+)(??{ your_condition ? "" : "(*FAIL)"})/ ? I mean, this is OK if you are writing some quick one-liner for a one time job; but if this is supposed to be maintainable, production code, then don't try to do this with just a regex of course ;)
    – Dada
    2 days ago












  • (peharps "(*ACCEPT)" instead of "". Both work, and I have no opinion on which is "better". ACCEPT is more explicit I guess; but since you are writing something not readable at all, does it really matter?)
    – Dada
    2 days ago










  • @dada yeah, it's gotta be a one liner. I'm injecting into some version matching thing into some script to help a network guy out using their own tools. But that totally worked, thanks a lot you want to make it a legit answer to get credit?
    – Peter Turner
    2 days ago


















Maybe using (??{}) instead of (?{}): /(.+).(.+)((.+))(.+)(??{ your_condition ? "" : "(*FAIL)"})/ ? I mean, this is OK if you are writing some quick one-liner for a one time job; but if this is supposed to be maintainable, production code, then don't try to do this with just a regex of course ;)
– Dada
2 days ago






Maybe using (??{}) instead of (?{}): /(.+).(.+)((.+))(.+)(??{ your_condition ? "" : "(*FAIL)"})/ ? I mean, this is OK if you are writing some quick one-liner for a one time job; but if this is supposed to be maintainable, production code, then don't try to do this with just a regex of course ;)
– Dada
2 days ago














(peharps "(*ACCEPT)" instead of "". Both work, and I have no opinion on which is "better". ACCEPT is more explicit I guess; but since you are writing something not readable at all, does it really matter?)
– Dada
2 days ago




(peharps "(*ACCEPT)" instead of "". Both work, and I have no opinion on which is "better". ACCEPT is more explicit I guess; but since you are writing something not readable at all, does it really matter?)
– Dada
2 days ago












@dada yeah, it's gotta be a one liner. I'm injecting into some version matching thing into some script to help a network guy out using their own tools. But that totally worked, thanks a lot you want to make it a legit answer to get credit?
– Peter Turner
2 days ago






@dada yeah, it's gotta be a one liner. I'm injecting into some version matching thing into some script to help a network guy out using their own tools. But that totally worked, thanks a lot you want to make it a legit answer to get credit?
– Peter Turner
2 days ago














1 Answer
1






active

oldest

votes

















up vote
4
down vote













First of all, the regex does succeed. But it does so whether the condition is true or not. I think you're actually asking for it to fail when the condition is false. For that, you want



(?(?{ condition })(*ACCEPT)|(*FAIL))


or just



(?(?{ !condition })(*FAIL))


Fixed:



perl -nle'
print "$_: match"
if /
^(.+).(.+)((.+))(.+)z
(?(?{
!( $1 > 9
|| $1 == 9 && $2 > 1
|| $1 == 9 && $2 == 1 && $3 > 7
|| $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
)
})(*FAIL))
/x;
' versions


A far better approach is to do the check outside of the pattern.



perl -nle'
print "$_: match"
if /^(.+).(.+)((.+))(.+)z/
&& (
$1 > 9
|| $1 == 9 && $2 > 1
|| $1 == 9 && $2 == 1 && $3 > 7
|| $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
);
' versions





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%2f53343632%2fhow-do-i-make-a-perl-regular-expresion-fail-in-the-code-block%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
    4
    down vote













    First of all, the regex does succeed. But it does so whether the condition is true or not. I think you're actually asking for it to fail when the condition is false. For that, you want



    (?(?{ condition })(*ACCEPT)|(*FAIL))


    or just



    (?(?{ !condition })(*FAIL))


    Fixed:



    perl -nle'
    print "$_: match"
    if /
    ^(.+).(.+)((.+))(.+)z
    (?(?{
    !( $1 > 9
    || $1 == 9 && $2 > 1
    || $1 == 9 && $2 == 1 && $3 > 7
    || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
    )
    })(*FAIL))
    /x;
    ' versions


    A far better approach is to do the check outside of the pattern.



    perl -nle'
    print "$_: match"
    if /^(.+).(.+)((.+))(.+)z/
    && (
    $1 > 9
    || $1 == 9 && $2 > 1
    || $1 == 9 && $2 == 1 && $3 > 7
    || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
    );
    ' versions





    share|improve this answer



























      up vote
      4
      down vote













      First of all, the regex does succeed. But it does so whether the condition is true or not. I think you're actually asking for it to fail when the condition is false. For that, you want



      (?(?{ condition })(*ACCEPT)|(*FAIL))


      or just



      (?(?{ !condition })(*FAIL))


      Fixed:



      perl -nle'
      print "$_: match"
      if /
      ^(.+).(.+)((.+))(.+)z
      (?(?{
      !( $1 > 9
      || $1 == 9 && $2 > 1
      || $1 == 9 && $2 == 1 && $3 > 7
      || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
      )
      })(*FAIL))
      /x;
      ' versions


      A far better approach is to do the check outside of the pattern.



      perl -nle'
      print "$_: match"
      if /^(.+).(.+)((.+))(.+)z/
      && (
      $1 > 9
      || $1 == 9 && $2 > 1
      || $1 == 9 && $2 == 1 && $3 > 7
      || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
      );
      ' versions





      share|improve this answer

























        up vote
        4
        down vote










        up vote
        4
        down vote









        First of all, the regex does succeed. But it does so whether the condition is true or not. I think you're actually asking for it to fail when the condition is false. For that, you want



        (?(?{ condition })(*ACCEPT)|(*FAIL))


        or just



        (?(?{ !condition })(*FAIL))


        Fixed:



        perl -nle'
        print "$_: match"
        if /
        ^(.+).(.+)((.+))(.+)z
        (?(?{
        !( $1 > 9
        || $1 == 9 && $2 > 1
        || $1 == 9 && $2 == 1 && $3 > 7
        || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
        )
        })(*FAIL))
        /x;
        ' versions


        A far better approach is to do the check outside of the pattern.



        perl -nle'
        print "$_: match"
        if /^(.+).(.+)((.+))(.+)z/
        && (
        $1 > 9
        || $1 == 9 && $2 > 1
        || $1 == 9 && $2 == 1 && $3 > 7
        || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
        );
        ' versions





        share|improve this answer














        First of all, the regex does succeed. But it does so whether the condition is true or not. I think you're actually asking for it to fail when the condition is false. For that, you want



        (?(?{ condition })(*ACCEPT)|(*FAIL))


        or just



        (?(?{ !condition })(*FAIL))


        Fixed:



        perl -nle'
        print "$_: match"
        if /
        ^(.+).(.+)((.+))(.+)z
        (?(?{
        !( $1 > 9
        || $1 == 9 && $2 > 1
        || $1 == 9 && $2 == 1 && $3 > 7
        || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
        )
        })(*FAIL))
        /x;
        ' versions


        A far better approach is to do the check outside of the pattern.



        perl -nle'
        print "$_: match"
        if /^(.+).(.+)((.+))(.+)z/
        && (
        $1 > 9
        || $1 == 9 && $2 > 1
        || $1 == 9 && $2 == 1 && $3 > 7
        || $1 == 9 && $2 == 1 && $3 == 7 && $4 > 22
        );
        ' versions






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        ikegami

        259k11172392




        259k11172392






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343632%2fhow-do-i-make-a-perl-regular-expresion-fail-in-the-code-block%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

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]