2

I have a table with a rather large number of items, all of which have I want to add images to. Take a look at the code I am working with.

<tr><td><img src=".jpg" width=100 height=100/></td></tr>

Now I know that I could manually input the name of each image but I am learning Regular expressions using Notepadd++ version 5.9.8, and I normally use the Find/Replace feature to update identical segments of code. My approach towards a simplified solution I thought I could number images from 1 all the way to the last image, and use Regular Expressions to insert 1,2,3... using Find and replace for the above code snippet.

I ofcourse would want the result to be added before the .jpg, and it should start at 1 for the first item, 2 for the second, and all the way until the last table item.

Dino Excel
  • 384
  • 7
  • 20
  • 1
    Without having something to replace, I can't see how RegEx is going to help. You would have to put into every `src` the number you want to make it work, which would make the replacement almost pointless (as you'll have to update each one manually anyway)... if that makes sense – freefaller Dec 18 '12 at 18:08
  • 1
    Plus RegEx and HTML aren't particularly good bed-fellows. As I once read "rule 1, don't use RegEx on HTML - rule 2, if you want to use RegEx on HTML see rule 1" – freefaller Dec 18 '12 at 18:09
  • You're not going to be able to do this in notepad++, you'll have no way to increment the replace text. Writing a script (PHP, PERL, PYTHON, nodeJS, etc.) to do this would be easy though. – Waxen Dec 18 '12 at 18:09
  • Here is the related answer: http://stackoverflow.com/questions/7602816/notepad-incrementally-replace. – VisioN Dec 18 '12 at 18:15

3 Answers3

1

That's not something a regex can do.

I usually do these things with excel. Put two tabs in the place where you want your number to be, and paste it in excel:

<tr><td><img src="  1   .jpg" width=100 height=100/></td></tr>
<tr><td><img src="  2   .jpg" width=100 height=100/></td></tr>

Expand the rows down until you have enough numbers -

enter image description here

Then just copy-paste them back to Notepad++ and replace all tabs (\t) with null.

P.S.

Update Notepad++ to the latest version, they switched to a more standard regex engine.

yellowblood
  • 1,604
  • 2
  • 17
  • 32
1

This is very simple to do in js/html, for instance.

<!DOCTYPE html>
<html>
    <head>
        <script>
        function OnLoad(){
            var text=0;
            for (var i=0; i<100; i++)
                text+= '<tr><td><img src="'+i+'.jpg" width=100 height=100/></td></tr>\n';

            document.getElementById("text").value= text;
            text='';
        };
        </script>
    </head>
    <body onload="OnLoad();">
        <textarea id="text" rows="30" cols="80"></textarea>
    </body>
<html>
mkey
  • 535
  • 2
  • 12
1

It's kind of silly, but here's a way you can do this in Notepad++ directly. Let's say you have the following HTML:

<tr><td><img src=".jpg" width=100 height=100/></td></tr>
<tr><td><img src=".jpg" width=100 height=100/></td></tr>
<tr><td><img src=".jpg" width=100 height=100/></td></tr>

What you can do is hold Alt and use you mouse to select (and drag down) a zero-character column between the first quote and the dot, in the src=".jpg sections. After doing so, your screen should look something like this (the | represents the cursor):

<tr><td><img src="|.jpg" width=100 height=100/></td></tr>
<tr><td><img src="|.jpg" width=100 height=100/></td></tr>
<tr><td><img src="|.jpg" width=100 height=100/></td></tr>

Now go to Edit -> Column Editor, select Number to insert, and give an initla value and increase value. Hit OK and you should see numbers inserted! Huzzah!

<tr><td><img src="1.jpg" width=100 height=100/></td></tr>
<tr><td><img src="2.jpg" width=100 height=100/></td></tr>
<tr><td><img src="3.jpg" width=100 height=100/></td></tr>

But wait, if you have both single-digit and double-digit numbers, you'll see a problem: there's extra whitespace!

...
<tr><td><img src="8 .jpg" width=100 height=100/></td></tr>
<tr><td><img src="9 .jpg" width=100 height=100/></td></tr>
<tr><td><img src="10.jpg" width=100 height=100/></td></tr>
<tr><td><img src="11.jpg" width=100 height=100/></td></tr>
...

Sounds like a job for Regex. Here's one that works (don't look if you want to figure one out for yourself):

Find: src="([0-9]+)\s+\.jpg Replace: src="$1\.jpg

voithos
  • 68,482
  • 12
  • 101
  • 116