I have massive html code, with loooads of images, problem is, every single image has a different path, example:
<img src="../media/2010/01/something.jpg" />
<img src="../media/logo.png" />
What I wanted to do with regular expressions is, to find every image path and replace it with:
<img src="../img/FILENAME.EXTENSION" />
I know that it's definately possible with regular expressions ... but it's just not my cup of tea, could any1 help me please?
Cheers, Mart
This might not be the best solution but it might work:
(<img.*?src=")([^"]*?(\/[^/]*\.[^"]+))
and then you use capture group 1 and 3 to create the new string (depending on flavor):
$1../img$3
You can see it in action here: http://regexr.com?2v8ir
If you want to parse html, its much better if you use an html parser instead of regex. There are quite alot of them and they do a very good work.
Html Agility Pack is a good one
Try this link
Using this regex <img src="[\w/\.]+"(\s|)/> and replacing with <img src="../img/FILENAME.EXTENSION" />
Related
I need to batch change a folder full of files, changing all image links to lower case and replacing underscores with dashes. Thus, <img src="/images/Maps/South_America.png"> would become <img src="images/maps/south-america.png">
I already performed similar operations on all local links in the same files. I used this regex to change them to lower case:
(?<=(?i)href=")((?:<\?php(?:(?!\?>).)+\?>)?)((?:'[^']+')?)([^"]+)(?=")
\1\2\L\3
And I used this one to replace underscores with dashes:
(href="(?!http)[^_"]+)\_([^"]+")
$1-$2
I'm not even sure if they're the same "language;" I think one only works in Dreamweaver, the other in TextWrangler. Anyway, I haven't figured out how to modify to match images, rather than links. I should emphasize that I only to change the image paths and names, not any classes, ID's or alt tags.
For example, <img src="Buffalo_Bill.jpg" alt="Buffalo Bill" class="People"> would become <img src="buffalo-bill.jpg" alt="Buffalo Bill" class="People">
Also, I think this covers all the bases if defining image extensions is necessary...
(?:jpe?g|gif|png|svg|swf)
The regexes I posted above are just examples. If you have a regex that's totally different, that's fine - just as long as it will work in a common text editor like Dreamweaver or TextWrangler. (I'm on a Mac.)
With an input like this:
<img id="BoringSnowDay" class="FunkySmellsFromGarden" src="/images/Maps/South_America.png" alt="Powerball Winner!" /> <img id="ExcitingSunNight" class="SmoothTasteInKitchen" src="/images/Flags/Antartica.jpg" alt="Racecar racecaR!" />
This regex in TextWrangler:
(<img [^>]+)(src="[^"]+")
Replace:
\1\L\2
Gives me something that ONLY affects the src="..." portion and nothing else.
Unfortunately, combining that to a "...and replace _ to -" tends to get a little tricky.
I've tried some solutions found in web, but it didn't help.
Given:
<p><img alt="" src="images/img2.jpg" style="float:left; height:300px; width:600px" /></p><p>bla-bla-bla</p>
I need to get:
images/img2.jpg.
Using now: preg_match('$<img.*src="(.*)"$', $text, $matches); and it does not give a result.
Use the regex: <img.*src="(.*)".*/>
This will match your image tags and the first capture group will give you your path. Your specific language may require some massaging of the regex.
In general, parsing tags with regex is not a good idea, however (if your tag spans lines it won't hit it, for instance).
I need to replace the below url (including img tags) with text. I am not very good with regex...
However, as the date will change its not only about copy and replace infortunately.
<img src="http://thailandsbloggare.se/wp-content/uploads/2012/10/icon_wink.gif" alt=";)">
and sometimes with class="wp-smiley"
<img src="http://thailandsbloggare.se/wp-content/uploads/2012/09/icon_wink.gif" alt=";)" class="wp-smiley" />
So any time this image is posted I want the complete string to replaced to text ";)"
Its a site built in Wordpress using PHP
Thanks in advance!
EDIT: I want to clarify why I can not just use a search and replace and why the url is dynamic.
The date part - written as this /2012/10/ will be different for every time this image is posted. So everything but the date will be the same all the time!!
And saometime the string ends with alt=";)"> and sometimes with class="wp-smiley" />
This should match your URL and any date (at least until 2099)
http://thailandsbloggare.se/wp-content/uploads/20\d\d/\d+/icon_wink\.gif
I need to replace the below url (including img tags) with text. I am not very good with regex... As you can see its dynamic with dates, and it ends in two different ways:
with alt=";)"> and sometimes with class="wp-smiley" />
<img src="http://thailandsbloggare.se/wp-content/uploads/2012/10/icon_wink.gif" alt=";)">
and sometimes with class="wp-smiley" at the end
<img src="http://thailandsbloggare.se/wp-content/uploads/2012/09/icon_wink.gif" alt=";)" class="wp-smiley" />
So any time this image is posted I want the complete string to replaced to text ";)"
I have managed to write the regex for everything until alt=";)"> and sometimes with class="wp-smiley" /> but then I am stuck, pressume need some OR functionality here.
<img src="http://thailandsbloggare.se/wp-content/uploads/20\d\d/\d+/icon_wink\.gif
Updated information after replies below
<img src="http://thailandsbloggare.se/wp-content/uploads/20[0-9]{2}/[01][0-9]/icon_wink.gif" alt=";\)" *(|class="wp-smiley")?>
and
Both fail returning strings whith class="wp-smiley" /> included
Its a site built in Wordpress using PHP and I am using http://urbangiraffe.com/plugins/search-regex/
Thanks in advance!
Normally, in a regex, you can create alternative sub-regexes:
(match this|or this)
In your case
(alt=";\)"|class="wp-smiley")
If alt=";)" is always there, do:
alt=";\)" *(|class="wp-smiley")
Of course, we don't know in which editor or programming language you are operating, and the actual regex implementation can be different from the above example.
Try the following pattern search:
<img src="http://thailandsbloggare.se/wp-content/uploads/20[0-9]{2}/[01][0-9]/icon_wink.gif" alt=";\)"(\sclass="wp-smiley")?>
Please refer to the syntax supported by the regex engine you are using. But, for most engines the above pattern should work. Note the character class used for date ranges, you should change it appropriately.
I need to process a HTML content and replace the IMG SRC value with the actual data. For this I have choose Regular Expressions.
In my first attempt I need to find the IMG tags. For this I am using the following expression:
<img.*src.*=\s*".*"
Then within the IMG tag I am looking for SRC="..." and replace it with the new SRC value. I am using the following expression to get the SRC:
src\s*=\s*".*"\s*
The second expression having issues:
For the following text it works:
<img alt="3D""" hspace=
"3D0" src="3D"cid:TDCJXACLPNZD.hills.jpg"" align=
"3dbaseline" border="3d0" />
But for the following it does not:
<img alt="3D""" hspace="3D0" src=
"3D"cid:UHYNUEWHVTSH.lilies.jpg"" align="3dbaseline"
border="3d0" />
What happens is the expression returns
src="3D"cid:TDCJXACLPNZD.hills.jpg"" align=
"3dbaseline"
It does not return only the src part as expected.
I am using C++ Boost regex library.
Please help me to figure out the problem.
Thanks,
Hilmi.
The problem is that .* is a "greedy" match - it will grab as much text as it possibly can while still allowing the regex to match. What you probably want is something like this:
src\s*=\s*"[^"]*"\s*
which will only match non-doublequote characters inside the src string, and thus not go past the ending doublequote.
Your first regex doesn't work on your sample text for me. I usually use this instead, when looking for specific HTML tags:
<img[^>]*>
Also, try this for your second expression:
src\s*=\s*"[^"]*"\s*
Does that help?