Regex - Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aeuglein

    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!
  • Justin Koivisto

    #2
    Re: Regex - Problem

    aeuglein wrote:
    [color=blue]
    > 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 ?[/color]

    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 - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Comment

    • Andy Hassall

      #3
      Re: Regex - Problem

      On 9 Oct 2003 05:35:23 -0700, sdz@gmx.de (aeuglein) wrote:
      [color=blue]
      >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 ?[/color]

      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::Ex plain 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 (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
      Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

      Comment

      • Andy Hassall

        #4
        Re: Regex - Problem

        On Thu, 09 Oct 2003 15:50:50 GMT, Justin Koivisto <spam@koivi.com > wrote:
        [color=blue]
        >aeuglein wrote:
        >[color=green]
        >> 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 ?[/color]
        >
        >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[/color]

        (\.(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 (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        • Justin Koivisto

          #5
          Re: Regex - Problem

          Andy Hassall wrote:
          [color=blue]
          > On Thu, 09 Oct 2003 15:50:50 GMT, Justin Koivisto <spam@koivi.com > wrote:
          >
          >[color=green]
          >>aeuglein wrote:
          >>
          >>[color=darkred]
          >>>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 ?[/color]
          >>
          >>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[/color]
          >
          >
          > (\.(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 :-)[/color]

          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 - spam@koivi.com
          PHP POSTERS: Please use comp.lang.php for PHP related questions,
          alt.php* groups are not recommended.

          Comment

          Working...