Nov 17, 2009 4:55 pm
Mangled_us@Yahoo.ComHi,
Can anyone tell me hoq to write a regular expression which matches
anything _except_ a litteral string ?
For instance, I want to match any line which does not begin with
Nomatch. So in the following :
Line1 xxxx
Line2 yyyy
Nomatch zzzz
Line3 aaaa
Line 4 bbbb
I would match every line except the one containing "Nomatch zzzz"
Many thanks,
David
Nov 18, 2009 3:42 pm
Dermot2009/11/17 mangled_us@yahoo.com <mangled_us@yahoo.com>:
> Hi,
Hello,
> Can anyone tell me hoq to write a regular expression which matches
> anything _except_ a litteral string ?
>
> For instance, I want to match any line which does not begin with
> Nomatch. ?So in the following :
>
> Line1 xxxx
> Line2 yyyy
> Nomatch zzzz
> Line3 aaaa
> Line 4 bbbb
>
> I would match every line except the one containing "Nomatch zzzz"
> anything _except_ a litteral string ?
>
> For instance, I want to match any line which does not begin with
> Nomatch. ?So in the following :
>
> Line1 xxxx
> Line2 yyyy
> Nomatch zzzz
> Line3 aaaa
> Line 4 bbbb
>
> I would match every line except the one containing "Nomatch zzzz"
You would negate the pattern. Something like this:
#!/usr/bin/perl
use strict;
use warnings;
while (<DATA>) {
print if ! /^Nomatch/;
}
__DATA__
Line1 xxxx
Line2 yyyy
Nomatch zzzz
Line3 aaaa
Line 4 bbbb
~
Output:
Line1 xxxx
Line2 yyyy
Line3 aaaa
Line 4 bbbb
see
perldoc perlop #Logical-Not
and
perldoc perlsyn
and of course
perldoc perlrequick
HTH,
Dp.
Nov 18, 2009 4:12 pm
Rob Coops--000e0cdfcdd6b1f82e0478a78389
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
On Wed, Nov 18, 2009 at 5:05 PM, Thomas B=C3=A4tzler <t.baetzler@bringe.com=
>wrote:
> Hi,
>
> Dermot <paikkos@googlemail.com> suggested:
>
>
> One could also use a zero-with negative look-ahead assertion:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> while( my $line =3D <DATA> ){
> if( $line =3D~ m/^(?!Nomatch)/ ){
> print "match: $line";
> }
> }
>
> __DATA__
> Line1 xxxx
> Line2 yyyy
> Nomatch zzzz
> Line3 aaaa
> Line 4 bbbb
>
> Cheers,
> Thomas
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
Look ahead notation works only on relatively recent versions of Perl, if>
> Dermot <paikkos@googlemail.com> suggested:
> > 2009/11/17 mangled_us@yahoo.com <mangled_us@yahoo.com>:
>> > > Can anyone tell me hoq to write a regular expression which matches
> > > anything _except_ a litteral string ?
> > >
> > > For instance, I want to match any line which does not begin with
> > > Nomatch. So in the following :
> > > anything _except_ a litteral string ?
> > >
> > > For instance, I want to match any line which does not begin with
> > > Nomatch. So in the following :
>
> > You would negate the pattern. Something like this:
> >
> > #!/usr/bin/perl
> >
> >
> > use strict;
> > use warnings;
> >
> > while (<DATA>) {
> > print if ! /^Nomatch/;
> > }
> >
> > __DATA__
> > Line1 xxxx
> > Line2 yyyy
> > Nomatch zzzz
> > Line3 aaaa
> > Line 4 bbbb
>> >
> > #!/usr/bin/perl
> >
> >
> > use strict;
> > use warnings;
> >
> > while (<DATA>) {
> > print if ! /^Nomatch/;
> > }
> >
> > __DATA__
> > Line1 xxxx
> > Line2 yyyy
> > Nomatch zzzz
> > Line3 aaaa
> > Line 4 bbbb
> One could also use a zero-with negative look-ahead assertion:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> while( my $line =3D <DATA> ){
> if( $line =3D~ m/^(?!Nomatch)/ ){
> print "match: $line";
> }
> }
>
> __DATA__
> Line1 xxxx
> Line2 yyyy
> Nomatch zzzz
> Line3 aaaa
> Line 4 bbbb
>
> Cheers,
> Thomas
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
your environment contains things like HP-UX that ships with a decades old
version of Perl 5.005 I believe it is (depending on the version of HP-UX of
course) you might get in trouble.
I would therefore not use it or make the script explicitly require 5.6 or
higher just in case.
Regards,
Rob
--000e0cdfcdd6b1f82e0478a78389--
Nov 18, 2009 4:05 pm
T BaetzlerHi,
Dermot <paikkos@googlemail.com> suggested:
> 2009/11/17 mangled_us@yahoo.com <mangled_us@yahoo.com>:
> > Can anyone tell me hoq to write a regular expression which matches
> > anything _except_ a litteral string ?
> >
> > For instance, I want to match any line which does not begin with
> > Nomatch. ?So in the following :
> > anything _except_ a litteral string ?
> >
> > For instance, I want to match any line which does not begin with
> > Nomatch. ?So in the following :
> You would negate the pattern. Something like this:
>
> #!/usr/bin/perl
>
>
> use strict;
> use warnings;
>
> while (<DATA>) {
> print if ! /^Nomatch/;
> }
>
> __DATA__
> Line1 xxxx
> Line2 yyyy
> Nomatch zzzz
> Line3 aaaa
> Line 4 bbbb
>
> #!/usr/bin/perl
>
>
> use strict;
> use warnings;
>
> while (<DATA>) {
> print if ! /^Nomatch/;
> }
>
> __DATA__
> Line1 xxxx
> Line2 yyyy
> Nomatch zzzz
> Line3 aaaa
> Line 4 bbbb
One could also use a zero-with negative look-ahead assertion:
#!/usr/bin/perl -w
use strict;
while( my $line = <DATA> ){
if( $line =~ m/^(?!Nomatch)/ ){
print "match: $line";
}
}
__DATA__
Line1 xxxx
Line2 yyyy
Nomatch zzzz
Line3 aaaa
Line 4 bbbb
Cheers,
Thomas
Previous Thread: Re: Net::SNMP OID doubt
Next Thread: Is unlink() supposed to provide an error message on failure?
Related Forum Topics
- Regular Expressions Question
- Regular Expressions Help
- Regular Expressions
- Help with regular expressions
- \w regular expressions unicode
- Can regular expressions be used as subroutine arguments?
- Comparing files with regular expressions
- Regular Expressions with Incremented Variable Embedded
- Parse::Recdescent match word exactly with regular expressions
- RE: Regular Expression Question