Question

Lets say I have a document that looks like the following

(001 [bla bla bla
bla bla bla])
(002 [bla
bla bla])
(003 [bla bla bla
bla bla bla])
(004 [bla
bla bla])
...
(199 [bla bla bla bla
bla])

So notice the open parenthesis with an three digits integer. Is there a text editor that is capable of adding a specific integer to all selected integers. In sublime text, I know how I could select all those integers by pressing ctrl+d after selecting the first open parenthesis a and then press the right arrow once and then press shift and right arrow three times to select all those integers. After selecting, is there a function in sublime text that could do a simple addition to those numbers. Say +10 so that the integers will be 011, 012, 013, ..., 219. If sublime text does not have this function, is there a text editor that has?

Was it helpful?

Solution

With the proviso that it's not strictly a text editor in the full sense and most certainly isn't SublimeText, my utility TextDistil will do this for you quite easily:

  1. Add a new "evaluate arithmetic expression" recipe with match '\d+' and arithmetic expression '1+$0' (Change '1' to whatever number you want to add.)
  2. Use "pad text of left" to pad '\d+' to a width of 3
  3. use 'replace text' to replace '( ' with '(00'
  4. use 'replace text' to replace '( ' with '(0'

If your input is a bit trickier than this you may need to get fancier with the regular expression matches.

Following the comment below, here's what you need to do to match strings like 'C11-001'...

You should use a matching expression of 'C11-(?< out>\d+)' with an arithmetic substitution of '48+$1' (Note there is not meant to be a space between the < and out but I can't get the markup to work without it.)

This may seem a bit cryptic so I'll try to explain the various parts....

  • C11- matches the first part of the strings you care about and means that 'bare' sets of digits will be unmatched and unaltered
  • The brackets in 'C11-(?< out>\d+)' mean that the thing that matches the term inside them will be counted as group 1 (group 0 is the entire thing including the C11-). That's why the arithmetic expression now says $1 instead of $0.
  • The odd-looking '?< out>' term is not part of the regular expression in the way you might think. Instead, it's a special syntax meaning that the matching term inside the brackets is a named group. (This is standard .Net regex syntax.) TextDistil assumed that the group you call 'out' is the one it should replace with the results of the arithmetic term. If the 'out' group is missing, it will use $0 which is the entire match which is why I didn't use it in the earlier example.

So to summarise the above, we're matching all strings that look like C11-001 etc, then feeding the digits after the hyphen to the arithmetic expression then substituting the results of that expression back into the 'out' group which happens to also be the digits after the hyphen.

As with the earlier example, you may need to add some extra steps if you want the resulting text to use exactly 3 digits after the hyphen. There are various ways to do this but I would suggest simply brute-forcing it by adding a couple of standard text substitutions like this....

  • Replace 'C11-(\d\d\D)' with C11-0$1 to bump 2 digit numbers up to 3
  • Replace 'C11-(\d\D)' with C11-00$1 to bump 1 digit numbers up to 3

OTHER TIPS

SynWrite editor supports Python scripting. So just make multi-selections, and write a Python plugin which can change selected numbers. Or you can write plugin which changes numbers w/out selections.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top