Anita Graham
Anita Graham

Reputation: 441

Can I use search/replace and regular expression to increment a counter?

I have a list of file names which I want to change to include a counter:

asdf.jpg to become  id:001, name:asdf.jpg
lkh.jpg  to become  id:002, name:lkh.jpg

In Aptana Studio 3 editor can I use a search/replace regular expression to increment the counter?

Thanks

Upvotes: 0

Views: 589

Answers (1)

Paul Walls
Paul Walls

Reputation: 6054

There is no way to do an indexed replace like you described using regular expressions. If your list is sequential, and not wrapped up in markup, then a quick script in any language could handle the replacements for you. For instance, in JavaScript...

(This code is quick and dirty, has no friends, and was teased in grade school, but gets the job done.)

<html>
<script>
function init(){
    var i,id=1,item,arr=[],items=document.body.innerHTML.split(/[\r\n]/g);
    for(i=0;i<items.length;i++){
        item=items[i].replace(/^\s+|\s+$/g,"");
        if(item=='')
            continue;
        arr[arr.length] = 'id:'+
            ('00'.substr(0,2-Math.floor(Math.log(id)/Math.LN10))+id)+
            ', name:'+item;
        id++;
    }
    document.write(arr.join('<br>'));
}
</script>
<body onload='init()'>
asdf.jpg
lkh.jpg
gfh.jpg
iuaa.jpg
(etc...)

Upvotes: 1

Related Questions