Sublime Text 2 increment numbers

2019-02-09 08:13发布

问题:

I have a JSON file that looks like this:

    "Algeriet" :
    [
        {
            "name" : "Nyårsdagen",
            "date" : "2013-01-01",
            "ID" : "1"
        },
        {
            "name" : "Mawlid En Nabaoui Echarif",
            "date" : "2013-01-24",
            "ID" : "2"
        },
        {
            "name" : "Första maj",
            "date" : "2013-05-01",
            "ID" : "3"
        },
       ...
     ]

Now I would like to begin incrementing the IDs from 0 instead of 1. How can I do this with Sublime Text 2? I have installed the Text Pastry plugin but I'm not sure how to select the IDs in the text so that I can replace these values.

回答1:

Solved it by doing these steps:

  1. Do a find and replace for regex "ID" : "\d+" and replacing it with a string which I know does not exist anywhere in the file (I replaced it with "ID" : "xyz"
  2. Make a multiple selection on "ID" : "xyz"
  3. Using the Text Pastry plugin "Number Sequence (\i)" on the multiple selection


回答2:

With the new add-text-with-sequence feature of the Text Pastry plugin, it's even a step less:

  1. Find all "ID" : "\d+" (with regex search activated)
  2. In the Text Pastry Command Line, enter as "ID" : "0"