471,306 Members | 837 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Regex - Problem

Hello!

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 ?

Thanks for help!

Sincerly!
Jul 17 '05 #1
4 2027
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 ?


Not being all that great with regex myself, I think this may do the
trick (or at least give you some ideas):

/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/](\.(jpg|gif|png|exe|zip|rar)){0})/i

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #2
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)
Jul 17 '05 #3
On Thu, 09 Oct 2003 15:50:50 GMT, Justin Koivisto <sp**@koivi.com> wrote:
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 ?


Not being all that great with regex myself, I think this may do the
trick (or at least give you some ideas):

/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/](\.(jpg|gif|png|exe|zip|rar)){0})/i


(\.(jpg|gif|png|exe|zip|rar)){0})

There's always a zero length match for this; zero occurrences of a pattern is
a zero length string, and there's plenty of those in between characters :-)

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #4
Andy Hassall wrote:
On Thu, 09 Oct 2003 15:50:50 GMT, Justin Koivisto <sp**@koivi.com> wrote:

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 ?


Not being all that great with regex myself, I think this may do the
trick (or at least give you some ideas):

/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/](\.(jpg|gif|png|exe|zip|rar)){0})/i

(\.(jpg|gif|png|exe|zip|rar)){0})

There's always a zero length match for this; zero occurrences of a pattern is
a zero length string, and there's plenty of those in between characters :-)


heh, I copied out of the wrong file... I wanted to make it into a
look-ahead (modified from something else I use)... oops

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by bill tie | last post: by
17 posts views Thread by clintonG | last post: by
3 posts views Thread by jg | last post: by
6 posts views Thread by Talin | last post: by
16 posts views Thread by Mark Chambers | last post: by
7 posts views Thread by =?Utf-8?B?amFj?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.