23. February 2019 at. 1:12 by Dr. Drang
One week or so ago I edited a program and decided that I should change some variable names. I thought it would be a simple regex find / replace and it was. Just not as simple as I thought. The variables were named
x10 and I wanted to switch them to
x30 ] respectively. I picked up BBEdit's Find window and entered it:
]because there were cases of
10in the code that were not related to the variables, and because I think I am smart, I would not do three non-regex substitutions, one each for
x10But I was not wise enough to notice the blue color of the replacement pattern. Had I done so, I would have seen that BBEdit interpreted my replacement pattern as "Captured Group 13, followed by
0" instead of "Captured Group 1, followed by
30", which was what I as the captive group 13 was blank, all became my variable names replaced with
You see, BBEdit can take up to 99 groups in search query The next, and strictly speaking, we should use two-digit numbers when referring to them in the replacement pattern. But in most cases we can use
9 instead of
because [thereisnoambiguityInotherwordsifIhadattemptedtochange would have been fine, because the subsequent
vz ] and
xz ] and a replacement pattern of
z means that there is no way to misinterpret the purpose of
in this pattern.
So after turning off the replacement, I changed the pattern to it,
<img class = "ss" width = "70%" src = "https://leancrew.com/all-this/images2019/ 20190223-Two-digit% 20BBEdit% 20replacement% 20pattern.png "alt =" Two-digit BBEdit replacement pattern "title =" Two-digit BBEdit replacement pattern "
and everything was right with the world.
There was another option: a named group. How it has seen, using
was as the pattern name:
I don't think I've ever used a named group in any situation, about the regex was in a text editor or a script t. My overall feeling is that if the pattern is so complicated, I have to use variables to keep track of all the groups. I should stop and break the problem down into smaller parts.
By the way, you may have heard that BBEdit is celebrating its 25th anniversary not to suck. When a well-documented app has such a long history, the manual begins to gather glorious recalls to the old days. When I looked up the notation for named groups in the BBEdit manual, I ran over this note: