Reputation: 129
So I have a database from a wordpress multisite. I'm doing a search and replace on a table with regex and need to make all src's of a specific image name (image2.jpg) point to a single directory. Here's an example. I may have:
src="http://domain.com/path/weird/different/image2.jpg
and
src="http://domain2.com/path2/differentpath/helloworld/image2.jpg
I need to replace everything between src="
and /image.jpg with a specific domain/filepath
.
I'm not great with regex stuff, I try, but it's just not my strong suit. Any help appreciated.
Upvotes: 0
Views: 63
Reputation: 41838
Search: src="[^"]*image2\.jpg
Replace: src="http://mydomain.com/mypath/image2.jpg
The [^"]*
eats up any characters that are not a double quote.
In the demo, see the substitutions pane at the bottom.
In PHP (should work with WordPress):
$replaced = preg_replace('/src="[^"]*image2\.jpg/',
'src="http://mydomain.com/mypath/image2.jpg',
$str);
Upvotes: 2
Reputation: 11041
Use this regex:
/(?<=src=")(.*?)(?=\Q/image2.jpg\E)/
This matches anything that goes in between "src="
" and "/image2.jpg
", so you are free to replace this with your specific domain/filepath
.
Depending on your language/ tool, you may have to escape the leading /
in /image2.jpg
.
> Positive Lookbehind - assert that match must be following 'src="'
|
| Anything > Positive Lookahead - assert that match
| | | is before '/image2.jpg' literally.
/(?<=src=")(.*?)(?=\Q/image2.jpg\E)/
Also try out an online regex tester.
Upvotes: 0