The regexp [^string] 
Author Message
 The regexp [^string]

  The regexp  [^f]  matches any charecter exept "f" , i.e. "f" is
  avoided.   Now: I want to avoid "foo", but [^foo] doesn't do the
  job.  So how do I avoid "foo" ( or a string ) in regexp ??

  hans

=========================================================================

LindMac A/S                                         Phone: +4773 96 73 11
7038 Trondheim,  NORWAY                             Fax:    . . . . 54 79
=========================================================================



Mon, 17 Jun 1996 20:54:25 GMT  
 The regexp [^string]

Quote:

>  The regexp  [^f]  matches any charecter exept "f" , i.e. "f" is
>  avoided.   Now: I want to avoid "foo", but [^foo] doesn't do the
>  job.  So how do I avoid "foo" ( or a string ) in regexp ??

Boy, I haven't seen this question recently.  ;^)

The most straightforward way I know of is to use separate regexps
for the negatives:

# Not foo
(! /foo/)

# foo followed by anything but bar
(/foo/ && ! /foo bar/)

# foo followed by (anything but bar or club) followed by baz
(/foo .* baz/ && ! /foo (bar|club)/)

For complicated constructs, this can add up quickly.  In Larry's last
post on this, he implied that he would consider adding negative
expressions, but certainly did not commit to it.

This may qualify as a FAQ soon (if it isn't there already -- I didn't
notice it).
--

"When the only tool you have is Perl, the whole | "Hooray for snakes!"
 world begins to look like your oyster." -- Me  |  -- The Simpsons (29 Apr 93)
------- "We _love_ being politically Koreshed" -- Steve Taylor, "Smug" -------



Wed, 19 Jun 1996 00:37:50 GMT  
 The regexp [^string]

Quote:

>   The regexp  [^f]  matches any charecter exept "f" , i.e. "f" is
>   avoided.   Now: I want to avoid "foo", but [^foo] doesn't do the
>   job.  So how do I avoid "foo" ( or a string ) in regexp ??

I assume that some character (say \000) is guaranteed not to occur
in the input.  Then it can be done as follows:

To replace every "bar" by "baz", unless it is followed by "foo":

  s/(bar)/$1\000/g;            # Put a marker after every bar.
  s/\000(foo)/$1/g;            # Remove the marker, if followed by foo.
  s/bar\000/baz/g;             # Replace every bar that is still followed
                               # by a marker.

To replace every "bar.*foo" by "baz", where the string that is matched
by '.*' does not contain any "foo":

  s/(foo)/\000$1/g;            # Put a marker before every foo.
  s/bar[^\000]*\000foo/baz/g;  # Replace every bar[<nomarker>]*<marker>foo .
  s/\000//g;                   # Remove remaining markers.

To check whether $_ contains some "bar" that is not (immediately)
followed by "foo":

  s/(bar)/$1\000/g;            # Put a marker after every bar.
  s/\000(foo)/$1/g;            # Remove the marker, if followed by foo.
  m/\000/;                     # Check whether there is a marker left.

Another possibility:

  s/barfoo/foo/g;              # Remove every bar that is followed by foo.
  m/bar/;                      # Check whether there is a bar left.

Note that the following command _does_something_different_:

  m/bar/ && ! m/barfoo/;       # Check that there is a bar and that
                               # *no* bar is followed by foo.

It will fail if the input contains both a bar that is followed by foo
and a bar that is *not* followed by foo.

--
Uwe Waldmann, Max-Planck-Institut fuer Informatik
Im Stadtwald, D-66123 Saarbruecken, Germany



Sat, 22 Jun 1996 00:54:28 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Extracting strings from postscript in a single regexp?

2. Regexp multiple string matches

3. Using a string for a regexp pattern

4. How to extract the toplevel-domain of a domainame from a string with regexp

5. negating a string in a regexp

6. searching for arbitrary literal string as opposed to regexp

7. Regexp to match a C-style string

8. (regexp) palindromes embedded in a string

9. Tricky: Generate matching string from regexp

10. Capitalizing a string with Regexp?

11. regexp to ignore strings and comments

12. Regexp fails matches @ string start

 

 
Powered by phpBB® Forum Software