Notepad++ RegEx replace with pattern - regex

I want to find the following pattern:
Image not found: /Images/IMG-20160519-WA0015.jpg
And replace with some markup, including the image name from the above text like:
<a href="IMG-20160519-WA0015.jpg"><img src="IMG-20160519-WA0015.jpg" width="180" height="240" alt="IMG-20160519-WA0015.jpg" class="image" />
Is it possible with some kind of Regex or plugin or I'm simply burning neurones?
Thanks.

Try finding ^Image not found: \/Images\/(IMG-.*\.jpg) and replacing with <a href="\1"><img src="\1" width="180" height="240" alt="\1" class="image" />
Note that the caret (^) in the regex says that it must be at the beginning of the line, not sure if that's the case for you but I suspect that it is. I also assumed that the "IMG-" prefix is constant, if not then you can just remove those four characters from the regex.
If you're not aware of it, RegExr is a nice interactive way to build and test regular expressions.
EDIT: Since you mentioned having trouble in the comments, here's an image of my settings:

Related

Replace a string using Notepad++ and regex

I have strings like this:
<img src="http://www.example.com/app_res/emoji/1F60A.png" /><img src="http://www.example.com/app_res/emoji/1F389.png" />
<img src="http://www.example.com/app_res/emoji/1F61E.png" /><img src="http://www.example.com/app_res/emoji/1F339.png" />
I want them to be like this:
😊 🎉
😞 🌹
In Notepad++, I tried this :
Find what: ^\s*<img src="http://www.example.com/app_res/emoji/(1F.*).png" />
Replace with: &#x\1;
The result is not as expected:
&#x1F60A.png" /><img src="http://www.example.com/app_res/emoji/1F389;
How to best isolate the regular expression ?
Any help is welcome ! Thank you
You're using the unspecific . together with the greedy star *. Don't do that here, as this tends to overshoot the target.
Be more specific.
The file name (in your case) does not contain dot's. Let's use "anything except a dot" ([^.]*) instead of "anything" (.*):
^\s*<img src="http://www.example.com/app_res/emoji/(1F[^.]*).png" />
You may try the following find and replace, in regex mode:
Find: <img src=".*?/([A-Z0-9]+\.\w+"\s*/><img src=".*?/([A-Z0-9]+\.\w+"\s*/>
Replace: &#x$1; &#x$2;
Here is a working regex demo.
Try
Find:^<.*?/(1\w+).*?/(1\w+).*
Replace:&#x$1; &#x$2;

Why does this regular expression work?

OK I'm thoroughly on why this regular expression works. The text I'm working with is this:
<html>
<body>
hello
<img src="withalt" alt="hi"/>asdf
<img src="noalt" />fdsaasdf
<img src="withalt2" alt="blah" />
</body>
</html>
Using the following regular expression (tested in php but I'm assuming it's true for all perl regular expressions), it will return all img tags which do not contain an alt tag:
/<img(?:(?!alt=).)*?>/
Returns:
<img src="noalt" />
So based on that I would think that simply removing the no backreference would return the same:
/<img(?!alt=).*?>/
Returns:
<img src="withalt" alt="hi"/>
<img src="noalt" />
<img src="withalt2" alt="blah" />
As you see instead it just returns all image tags. Then to make things even more confusing, removing the ? (simply a wildcard as far as I'm aware) after the * returns up to the final >
/<img(?!alt=).*>/
Returns:
<img src="withalt" alt="hi"/>
<img src="noalt" />fdsaasdf
<img src="withalt2" alt="blah" />
So anyone care to inform me, or at least point me in the right direction of what's going on here?
/<img(?:(?!alt=).)*?>/
This regex applies negative look-ahead for each character it matches after img. So, as soon as it finds alt=, it stops. So, it will only match the img tag, that does not have an alt attribute.
/<img(?!alt=).*?>/
This regex, just applies the negative look-ahead after img. So, it will match everything till the first > for all the img tag which is not followed by alt=, no matter whether alt= appears anywhere further down the string. It will be covered in .*?
/<img(?!alt=).*>/
This is same as the previous one, but it matches everything till the last >, since it uses greedy matching. But I don't know why you got that output. You should have got everything till the last > for </html>.
Now forget everything that happened there, and move towards an HTML Parser, for parsing an HTML. They are specifically designed for this task. So, don't bother using regex, because you can't parse every kind of HTML's through regex.

Need to replace dynamic image tag with text

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.

Regex: Match <img> tag if width or height is smaller than 100px

i would love to get rid of some tiny images in a rssfeed by matching and removing them with Apples NSRegularExpressions.
<img src="somepic" height="1" width="1"> should be matched for removal
<img src="somepic" height="50" width="100"> -> should also be matched
<img src="somepic" height="100" width="100"> -> this one should not be matched
my current approach is not working yet
<img(\s*[height|width]\s*=\s*"([0-9]|[1-9][0-9])"\s*+|[^>]+?)*>
my guess is there is some problem with the capture groups (which are probably not needed at all). Does anyone have a hint why its not working?
Try this regex:
<img[^>]*(?:height|width)\s*=\s*"[1-9]?[0-9]"[^>]*>
It fixes the small issues you had, that Mattias Buelens mentioned in his comment.
See on rubular.
This is in c# regex
(?<=<img).*?(height="([0-9]|[1-9][0-9])".*?width="([0-9]|[1-9][0-9])"|width="([0-9]|[1-9][0-9])".*?height="([0-9]|[1-9][0-9])").*?(?=>)
Hope this helps..

Regular expression to remove <p> tags around elements wrapped in [...]'s

I'm a total regexp noob. I'm working with wordpress and I'm desperately trying to deal with wordpress's wautop, which I hate and love (more hate!). Anyways I'm trying to remove <p> tags around certain commands.
Here's what I get:
<p>
[hide]
<img.../>
[/hide]
</p>
or
<p>
[imagelist]
<img .../>
<img .../>
[/imagelist]
</p>
Here's what I'd like:
[hide]
<img.../>
[/hide]
or
[imagelist]
<img .../>
<img .../>
[/imagelist]
I've tried:
preg_replace('/<p[^>]*>(\[[^>]*\])<\/p[^>]*>/', '$1', $content); // No luck!
EDIT:
When I am doing the regexp it is still just a variable containing text.. It is not parsed as html yet. I know it is possible because I already did it with getting rid of p tags around an image tag. So I just need a regexp to handle text that will be parsed as html at some point in the future.
Here's a similar question
Thanks!
Matt Mueller
You can't use regular expressions to parse HTML, because HTML is, by definition, a non-regular language. Period, end of discussion.
The language of matching HTML tags is context-free, not regular. This means regular expressions are probably not the right tool to use here. Context-free languages require parsers rather than regular expressions. So, you can either remove ALL <p> and </p> tags with a regular expression, or you can use an HTML parser to remove matching tags from certain parts of your document.
Try this regex:
'%<p[^>]*>\s*(\[([^\[\]]+)\].*?\[/\2\])\s*</p>%s'
Explanation:
\[([^\[\]]+)\] matches the opening bbcode tag and captures the tag name in group #2.
\[/\2\] matches a corresponding losing tag.
.*? matches anything, reluctantly. Thanks to the s flag at the end, it also matches newlines. The effect of the reluctant .*? is that it stops matching the first time it finds a closing bbcode tag with the right name. If tags are nested (within tags with the same name) or improperly balanced, it won't work correctly. I wouldn't expect that be a problem, but I have no experience with WordPress, so YMMV.