Perl Forum for Beginners

Regular expressions question

Nov 17, 2009 4:55 pm
Mangled_us@Yahoo.Com

Hi,

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
Dermot
Re: Regular expressions question

2009/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"


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
Re: Regular expressions question

--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:
> > 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 :
>
>
> > 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
>
> 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
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 Baetzler
AW: Regular expressions question

Hi,

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 :


> 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

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
Dear All,

This is more of a generic question on regular expressions as my
program is working fine but I was just curious.

Say you have the following URLs:

http://www.test.com/image.gif
http://www.test.com/?src=image.gif?width=12

I want to get the type of the image, i.e. the string...
Regular Expressions Help
I need to find out how to do this:


I have this: 95090dd272aae429bab4

and want it to pick up a hidden .flv file with a / in between.

example

95090dd272aae429bab4/33211.flv


How do I write out the regular expression for /then random numbers
then .flv?


any help would be...
Regular Expressions
Hi All,

I am a noob in Perl and hence would like some help to what I am sure is a very easy problem.

I have got a text file in csv format
The format is:
<TICKER>,<DATE>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOLUME>,<OI>

Now my objective is to change the format of the date, and rename the whole...
Help with regular expressions
--0015174410d68388f004a2dbcefb
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi List,

I am trying to write a small script to parse bibliographic references like
this:

Morgan, M.J., Wilson, C.E., Crim, L.W., 1999. The effect of stress...
\w regular expressions unicode
Hi how to write regular expressions matching against Unicode (eg.,
UTF-8) strings?

For instance, in my regexp:

qr/^([.<>@ \w])*$/

I am using \w because here: http://perldoc.perl.org/perlretut.html it says:

=======================================================================
\w...
Can regular expressions be used as subroutine arguments?
Hello Folks,

I need to make a substitution in place for each element of an array, and I need
to do this to two arrays. Currently the relevant code fragment (without pragmas) is:

--------
foreach my $element (@cddb_artist)
{
$element =~ s/^.*?([0-9,a-f]{8}):.*$/$1/;
...
Comparing files with regular expressions
Hello,

I'm a Perl uber-novice and I'm trying to compare two files in order to
exclude items listed on one file from the complete list on the other
file. What I have so far prints out a third file listing everything
that matches the exclude file from the complete file (which I'm...
Regular Expressions with Incremented Variable Embedded

I am using regular expressions to alter a text file. Where my original file has three spaces to start a paragraph, I want to replace each instance of three spaces with a bracketed paragraph number, with a counter for paragraph numbers, <pgf 1>, <pgf 2>, <pgf 3> etc. The PERL program that I'm...
Parse::Recdescent match word exactly with regular expressions
Hi,

( using parse::recdescent 1.94 )
I want to parse a string and search for a word (and exactly this word). I
use the following grammar:

my $grammar = <<'_EOGRAMMAR_';

any : /[a-zA-Z0-9\d\., +-]+/

cmd_unknown : any(s)
{ main::cmd_unknown(); $return = 1;}

cmd_doit : /doit/
{...
RE: Regular Expression Question
Raymond Wan <rwan@kuicr.kyoto-u.ac.jp> wrote:
> I looked a bit for some etiquette list for this mailing list
> and couldn't find out. Perhaps it's out there somewhere?

You could argue that in absence of any other rules, RFC 1855 applies:

"If you are sending a reply to a message or a...