0

I am using following Regex:-

\[img](.*.;base64.*.?)\[\/img]/mi

Now the sample text that I have is

This is some sample text. ([URL]http://www.example.com[/URL]) This is a sample page. The issues with this page are in following list.[LIST][*]Issue 1[IMG]http://www.example.com/image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkBAMAAACCzIhnAAAAG1BMVEXMzMyWlpacnJy+vr6jo6PFxcW3t7eqqqqxsbHbm8QuAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAiklEQVRYhe3QMQ6EIBAF0C+GSInF9mYTs+1ewRsQbmBlayysKefYO2asXbbYxvxHQj6ECQMAEREREf2NQ/fCtp5Zky6vtRMkSJEzhyISynWJnzH6Z8oQlzS7lEc/fLmmQUSvc16OrCPqRl1JePxQYo1ZSWVj9nxrrOb5esw+eXdvzTWfTERERHRXH4tWFZGswQ2yAAAAAElFTkSuQmCC[/IMG][IMG]https://www.example.com/attachment/random_img.png[/IMG][IMG]http://www.example.com/image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWBAMAAADOL2zRAAAAG1BMVEXMzMyWlpaqqqq3t7fFxcW+vr6xsbGjo6OcnJyLKnDGAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABAElEQVRoge3SMW+DMBiE4YsxJqMJtHOTITPeOsLQnaodGImEUMZEkZhRUqn92f0MaTubtfeMh/QGHANEREREREREREREtIJJ0xbH299kp8l8FaGtLdTQ19HjofxZlJ0m1+eBKZcikd9PWtXC5DoDotRO04B9YOvFIXmXLy2jEbiqE6Df7DTleA5socLqvEFVxtJyrpZFWz/pHM2CVte0lS8g2eDe6prOyqPglhzROL+Xye4tmT4WvRcQ2/m81p+/rdguOi8Hc5L/8Qk4vhZzy08DduGt9eVQyP2qoTM1zi0/uf4hvBWf5c77e69Gf798y08L7j0RERERERERERH9P99ZpSVRivB/rgAAAABJRU5ErkJggg==[/IMG][*]Issue 2[IMG]http://www.example.com/image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8BAMAAADI0sRBAAAAJFBMVEXMzMyWlpbFxcWjo6O+vr63t7ecnJy0tLSenp6Xl5exsbGqqqqVdOLvAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAXUlEQVQ4jWNgGAWjYBTQByibGzsAKSbjYhNs0iYaLgZAitmpxRmbdHKikgCXtwKbmZAiNmlRRyUBBlMGxhDs0kmaLgZMVgxsSlOwGu4MdBq3tgILDqeNglEwCrADAH+uCwDFhiuPAAAAAElFTkSuQmCC[/IMG][IMG]https://www.example.com/attachment/another_random_image.jpg[/IMG][IMG]http://www.example.com/image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyBAMAAADsEZWCAAAAG1BMVEXMzMyWlpaqqqq3t7exsbGcnJy+vr6jo6PFxcUFpPI/AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAQUlEQVQ4jWNgGAWjgP6ASdncAEaiAhaGiACmFhCJLsMaIiDAEQEi0WXYEiMCOCJAJIY9KuYGTC0gknpuHwXDGwAA5fsIZw0iYWYAAAAASUVORK5CYII=[/IMG][/LIST]

So the case is this regex is not matching all the occurances of those Base64 strings that are in individual [IMG] BBCodes but it counts all as one. Can someone point me where I am wrong? I just need to match those [IMG] BBCodes in which there is Base64 encoded content which I can identify through ;base64 substring match.

khalid
  • 367
  • 2
  • 15
  • Use `'~\[img]((?:(?!\[img]).)*?;base64.*?)\[/img]~si'`, see [demo](https://regex101.com/r/7JWORv/2). – Wiktor Stribiżew Mar 14 '19 at 13:57
  • "[" has special meaning in regexp, so you should probably protect them. And you may use regex101.com to test your regex. Try : https://regex101.com/r/ROWSee/1 – Armage Mar 14 '19 at 13:58
  • @WiktorStribiżew - Just tested - yours works perfectly. Also following one does work as well `/\[IMG\](.*?;base64.*?)\[\/IMG\]/mi` - so what's the difference between the two? – khalid Mar 14 '19 at 14:12
  • `\[IMG\](.*?;base64.*?)\[\/IMG\]` [does not work](https://regex101.com/r/OfB8CM/1), see the second match. – Wiktor Stribiżew Mar 14 '19 at 14:13
  • Hi @WiktorStribiżew, this regex `'~\[img]((?:(?!\[img]).)*?;base64.*?)\[/img]~si'` is giving me "Catastrophic Backtracking" for several images. What could be the reason? – khalid Mar 18 '19 at 05:51
  • @khalid Because you should actually use a BBcode parser. We can further [fine tune](https://regex101.com/r/7JWORv/3) the regex, but is it worth it? – Wiktor Stribiżew Mar 18 '19 at 08:25

0 Answers0