regex to strip out image urls?

2019-07-25 14:19发布

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.

4条回答
放我归山
2楼-- · 2019-07-25 14:38

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 :)

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-25 14:46

Try this...

(http://)([a-zA-Z0-9\/\\.])*
查看更多
Anthone
4楼-- · 2019-07-25 14:49

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

查看更多
聊天终结者
5楼-- · 2019-07-25 14:54

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

/(?<=")[^"\n]+/
查看更多
登录 后发表回答