Unix Programming - How to not match a string in regex?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2004 > How to not match a string in regex?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to not match a string in regex?
Neil Aggarwal

2004-03-25, 5:41 pm

Hello:

I am trying to write a regex pattern to not match a string.

I want my pattern to match anything that ends in .ameritech.net
but not .ded.ameritech.net

Is there a way to write:
\.[Any word except "ded"]\.ameritech\.net$

Thanks,
Neil
John W. Krahn

2004-03-25, 10:35 pm

Neil Aggarwal wrote:
>
> I am trying to write a regex pattern to not match a string.
>
> I want my pattern to match anything that ends in .ameritech.net
> but not .ded.ameritech.net
>
> Is there a way to write:
> \.[Any word except "ded"]\.ameritech\.net$


If you are using PERL or a PERL compatible RE library you can use a zero-width
negative look-behind assertion.

$ PERL -le'
for ( qw[ www.ameritech.net ded.ameritech.net www.ded.ameritech.net ded.one.ameritech.net ] ) {
print "$_ is a match" if /(?<!\bded)\.ameritech\.net$/;
}
'
www.ameritech.net is a match
ded.one.ameritech.net is a match



John
--
use Perl;
program
fulfillment
Nick Landsberg

2004-03-25, 10:35 pm

Neil Aggarwal wrote:

> Hello:
>
> I am trying to write a regex pattern to not match a string.
>
> I want my pattern to match anything that ends in .ameritech.net
> but not .ded.ameritech.net
>
> Is there a way to write:
> \.[Any word except "ded"]\.ameritech\.net$
>
> Thanks,
> Neil


grep "ameritech.net$" < file | grep -v "ded.ameritech.net" > file2


--
"It is impossible to make anything foolproof
because fools are so ingenious"
- A. Bloch
Juergen Pfann

2004-03-26, 11:46 am

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Neil Aggarwal wrote:
> Hello:
>
> I am trying to write a regex pattern to not match a string.
>
> I want my pattern to match anything that ends in .ameritech.net
> but not .ded.ameritech.net
>
> Is there a way to write:
> \.[Any word except "ded"]\.ameritech\.net$
>


Hmm, probably there is a way.
But I'd suggest -instead of a complex and hard-to-read "all in one"
regex-, to split up your problem into two simpler steps.
Why not eliminate the lines matching ".ded.ameritech.net" by a
"grep -v", and then apply a simpler regex to the remaining lines?

Just my 2¢.

Juergen
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with netscape - http://enigmail.mozdev.org

iEYEARECAAYFAkBjosMACgkQvVdsrqIiIr/2swCgzSL0xSzuTL+9rZHZHUYYmcbj
ESsAoIDKqZ+3Yrk7wFrEJxoPBuFUsdjy
=oaOC
-----END PGP SIGNATURE-----

stanislav shalunov

2004-03-26, 11:46 am

neil@JAMMConsulting.com (Neil Aggarwal) writes:

> I want my pattern to match anything that ends in .ameritech.net
> but not .ded.ameritech.net


Using extended regexp syntax:
([^.]ded|[^d]ed|[^e]d|[^d])\.ameritech\.net$

--
Stanislav Shalunov http://www.internet2.edu/~shalunov/
Neil Aggarwal

2004-03-28, 11:38 pm

Juergen:

I think you are right, I split it into two, I was hoping there was
a syntax for non-matches of a sequence of characters.

Thanks,
Neil


Juergen Pfann <juergen.pfann@t-online.de> wrote in message news:<ar704c.fjr.ln@news.t-online.com>...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Neil Aggarwal wrote:
>
> Hmm, probably there is a way.
> But I'd suggest -instead of a complex and hard-to-read "all in one"
> regex-, to split up your problem into two simpler steps.
> Why not eliminate the lines matching ".ded.ameritech.net" by a
> "grep -v", and then apply a simpler regex to the remaining lines?
>
> Just my 2¢.
>
> Juergen
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.3 (GNU/Linux)
> Comment: Using GnuPG with netscape - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkBjosMACgkQvVdsrqIiIr/2swCgzSL0xSzuTL+9rZHZHUYYmcbj
> ESsAoIDKqZ+3Yrk7wFrEJxoPBuFUsdjy
> =oaOC
> -----END PGP SIGNATURE-----

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com