boysenberry
boysenberry

Reputation: 87

regex to strip out image urls?

I need to separate out a bunch of image urls from a document in which the images are associated with names like this:

bellpepper = "http://images.com/bellpepper.jpg"
cabbage = "http://images.com/cabbage.jpg"
lettuce = "http://images.com/lettuce.jpg"
pumpkin = "http://images.com/pumpkin.jpg"

I assume I can detect the start of a link with:

/http:[^ ,]+/i

But how can I get all of the links separated from the document?

EDIT: To clarify the question: I just want to strip out the URLs from the file minus the variable name, equals sign and double quotes so I have a new file that is just a list of URLs, one per line.

Upvotes: 1

Views: 1267

Answers (4)

Nakilon
Nakilon

Reputation: 35064

You may try this, if your tool supports positive lookbehind:

/(?<=")[^"\n]+/

Upvotes: 0

user110714
user110714

Reputation:

Try this...

(http://)([a-zA-Z0-9\/\\.])*

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342333

do you mean to say you have that kind of format in your document and you just want to get the http part? you can just split on the "=" delimiter without regex

$f = fopen("file","r");
if ($f){
    while( !feof($f) ){
        $line = fgets($f,4096);
        $s = explode(" = ",$line);
        $s = preg_replace("/\"/","",$s);
        print $s[1];
    }
    fclose($f);
}

on the command line :

#php5 myscript.php > newfile.ext

if you are using other languages other than PHP, there are similar string splitting method you can use. eg Python/Perl's split(). please read your doc to find out

Upvotes: 0

Wojciech Bederski
Wojciech Bederski

Reputation: 3922

If the format is constant, then this should work (python):

import re
s = """bellpepper = "http://images.com/bellpepper.jpg" (...) """
re.findall("\"(http://.+?)\"", s)

Note: this is not "find an image in a file" regexp, just an answer to the question :)

Upvotes: 1

Related Questions