R
Ronny
If I want to express that a variable $v does NOT match some regular
expression RE,
I usually write this as
$v !~ /RE/ and print "string does not contain pattern\n"
Is there an easy way to write this in a positive way, i.e using $v =~
/.../ ?
I thought about using some of the zero-width lookahead operators, such
as
$v =~ /($?RE)/ # DOES NOT WORK
but this does not work of course, because in general, somewhere within
$v *will* be a position where RE would not match, even if RE would
match
at some other position.
Background of what this is needed for: I'm writing tiny utilities in
Perl, which
act as a filter for input text. Typically, the core of the "program"
contains
something like
/$PATTERN/ && print(transform($_))
i.e. read all lines from stdin, and if they match some pattern, print
out a transformed
version of the line. The is supplied via ARGV. This works fine, but I
also would like
the user of this utility to be able to *revert* the sense (i.e. read
all lines from stdin,
and if they DO NOT match the pattern, etc.), like you have with grep
(where the
option -v reverts the test). The keypoint here is that in this
particular application,
I would prefer NOT to introduce an option such as grep's "-v" to my
utility, but encode
the "negation of the pattern" into the pattern itself.
Is this possible at all within the realm of Perl regular expressions,
or do I have
to invent my own workaround (which of course would be possible)?
expression RE,
I usually write this as
$v !~ /RE/ and print "string does not contain pattern\n"
Is there an easy way to write this in a positive way, i.e using $v =~
/.../ ?
I thought about using some of the zero-width lookahead operators, such
as
$v =~ /($?RE)/ # DOES NOT WORK
but this does not work of course, because in general, somewhere within
$v *will* be a position where RE would not match, even if RE would
match
at some other position.
Background of what this is needed for: I'm writing tiny utilities in
Perl, which
act as a filter for input text. Typically, the core of the "program"
contains
something like
/$PATTERN/ && print(transform($_))
i.e. read all lines from stdin, and if they match some pattern, print
out a transformed
version of the line. The is supplied via ARGV. This works fine, but I
also would like
the user of this utility to be able to *revert* the sense (i.e. read
all lines from stdin,
and if they DO NOT match the pattern, etc.), like you have with grep
(where the
option -v reverts the test). The keypoint here is that in this
particular application,
I would prefer NOT to introduce an option such as grep's "-v" to my
utility, but encode
the "negation of the pattern" into the pattern itself.
Is this possible at all within the realm of Perl regular expressions,
or do I have
to invent my own workaround (which of course would be possible)?