On 9 Oct 2003 05:35:23 -0700,
sd*@gmx.de (aeuglein) wrote:
I have this RegEx:
/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i
Now, I want to exlude on the end of a String the formats .gif / .jpg /
.png / .exe / .zip / .rar
How I can this add to my regex ?
Assuming Perl-compatible regexes due to the use of \w.
If you want it in one regex, add a zero-width negative look-ahead assertion to
the end.
(Completely untested:)
/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/]).*(?!\.(?:gif|jpg|png|exe|zip|rar))$/i
The useful Perl module YAPE::Regex::Explain comes out with this explanation:
The regular expression:
(?-imsx:/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/]).*(?!\.(?:gif|jpg|png|exe|zip|rar))$/i)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
/ '/'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[\w]+ any character of: word characters (a-z,
A-Z, 0-9, _) (1 or more times (matching
the most amount possible))
----------------------------------------------------------------------
: ':'
----------------------------------------------------------------------
\/ '/'
----------------------------------------------------------------------
\/ '/'
----------------------------------------------------------------------
[\w-?&;#~=\.\/\@]+ any character of: word characters (a-z,
A-Z, 0-9, _), '-', '?', '&', ';', '#',
'~', '=', '\.', '\/', '\@' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
[\w\/] any character of: word characters (a-z,
A-Z, 0-9, _), '\/'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
gif 'gif'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
jpg 'jpg'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
png 'png'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
exe 'exe'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
zip 'zip'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
rar 'rar'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
/i '/i'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (
http://www.andyh.co.uk)
Space: disk usage analysis tool (
http://www.andyhsoftware.co.uk/space)