简体   繁体   中英

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

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...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM