Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Regex to match non image urls

by muba (Priest)
on Jan 18, 2013 at 09:12 UTC ( #1013991=note: print w/ replies, xml ) Need Help??


in reply to Regex to match non image urls

[^jpg] is a character class - meaning you match either j, p, org.

Update: and the ^ negates the character class, so [^jpg] matches a single character that is not j, p, or g.

Other than that, I feel your list is incomplete. Why won't you allow .svg images?

And are you aware that it's completely possible to write a script in, say some CGI capable language such as Perl indeed, that would be called my-image-script.(txt|html|bananajuice) that would still generate a valid image in whatever format? But your regexp would disallow it. Is that intentional?

Anyway, this should help you along...

m( \[img\] # Match opening tag \s* # Maybe there are spaces inside .*? # Non-greedy match \. # A dot (?: # Non-capturing group jpe?g | png | svg | gif | bmp ) # End of group \s* # Maybe there are more spaces still \[/img\] # Match closing tag )x


Comment on Re: Regex to match non image urls
Select or Download Code
Replies are listed 'Best First'.
Re^2: Regex to match non image urls
by userdefinable (Initiate) on Jan 18, 2013 at 09:29 UTC
    Thanks, not quite sure how to use this in the context of replacing occurrences in a block of text.
      Basically this doesn't work:
      $text =~ s~(\[img\]\s*.*?\.(?:jpe?g|png|svg|gif|bmp)\s*\[/img\])~Inval +id~isg;
        You want to use Parse::BBCode, but you could try
        $text =~ s{ \[(\w+)\] # $1 tagname ( .*? ) # $2 stuff \[\/(\w+)\] # $3 tagname }{ TagFudge( $1, $2, $3 ); }gsex; sub TagFudge { my( $open, $content, $close ) = @_; if( $content =~ m{\.(jpe?g|gif|xbm|png|bmp)$} ){ return "Ok"; } else { return "Invalid"; } }
        But you should use Parse::BBCode

        "Doesn't work" is not a problem description.

        Are you sure $text is what you think it is? You can try to do a print "\n>$text<\n"; and see if it matches your expectations.

        If $text is what you think it is, then how doesn't it work? What is your input, what is your expected output, what is your actual output? And with "actual output" I also mean warnings and errors. On that topic, is your script running with use strict; use warnings;?

        Right now it's doing the exact opposite of what I want it to!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013991]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (19)
As of 2015-07-30 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls