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.
perl pcre
add a comment |
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.
perl pcre
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
add a comment |
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.
perl pcre
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
perl pcre
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited 2 days ago
answered 2 days ago
ikegami
259k11172392
259k11172392
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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