Regex that matches anything except a specific string

  • Thread starter Richard Anderson
  • Start date
R

Richard Anderson

I am trying to feed a regular expression to a mail processing program that
matches every string except a specific string. For example, I want a regex
that does not match (e-mail address removed) but matches any other
string. If I were writing a Perl program, I would just do this:

if ($address !~ /^richard\@richard-anderson\.org$/) { print "Spam\n" }

but I am not able to modify the source code of the program I am using.

I haven't been able to do this using the complement metacharacter (^). I'd
accept a solution that matches anything except an anagram of
(e-mail address removed)

P.S. This is actually a Python regex, but Python's regexes are very similar
to Perl's regexes.
 
D

Dave Oswald

Richard Anderson said:
I am trying to feed a regular expression to a mail processing program that
matches every string except a specific string. For example, I want a regex
that does not match (e-mail address removed) but matches any other
string. If I were writing a Perl program, I would just do this:

if ($address !~ /^richard\@richard-anderson\.org$/) { print "Spam\n" }

but I am not able to modify the source code of the program I am using.

I haven't been able to do this using the complement metacharacter (^). I'd
accept a solution that matches anything except an anagram of
(e-mail address removed)

P.S. This is actually a Python regex, but Python's regexes are very similar
to Perl's regexes.

Your question has nothing to do with Perl, and the Perlish solution won't
work for Python unless Python's regexp is identical to Perl's. "Very
similar" isn't close enough.

That said, in Perl you would write a regexp to look like this:
/^(?!richard\@richard-anderson\.org)$/

That's a negative lookahead assertion.
 
J

Jeff 'japhy' Pinyan

I am trying to feed a regular expression to a mail processing program that
matches every string except a specific string. For example, I want a regex
that does not match (e-mail address removed) but matches any other
string. If I were writing a Perl program, I would just do this:

if ($address !~ /^richard\@richard-anderson\.org$/) { print "Spam\n" }

but I am not able to modify the source code of the program I am using.

I don't usually answer non-Perl questions in this newsgroup, but I'll
pretend you're asking how to turn

if ($str !~ /^pat$/) { ... }

into a match using =~ instead.

if ($str =~ /^(?!richard\@richard-anderson\.org$)/) { ... }
 
B

Bill

I am trying to feed a regular expression to a mail processing program that
matches every string except a specific string. For example, I want a regex
that does not match (e-mail address removed) but matches any other
string. If I were writing a Perl program, I would just do this:

if ($address !~ /^richard\@richard-anderson\.org$/) { print "Spam\n" }

but I am not able to modify the source code of the program I am using.

I haven't been able to do this using the complement metacharacter (^). I'd
accept a solution that matches anything except an anagram of
(e-mail address removed)

P.S. This is actually a Python regex, but Python's regexes are very similar
to Perl's regexes.

Though I don't know if python supports them, look at zero-with
look-ahead, NEGATIVE assertions, for example,

/^.?(?!richard\@richard-anderson\.org)/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top