قالب وردپرس درنا توس
Home / Mac / Regex groups and numbers – All this

Regex groups and numbers – All this



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 a10 v10 and x10 and I wanted to switch them to a30 v30 and x30 ] respectively. I picked up BBEdit's Find window and entered it:

 Error BBEdit replacement pattern "title =" Error BBEdit replacement pattern "/> </p>
<p>  I just couldn't replace <code> 1<div class=

0 with ] because there were cases of 10 in 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 a10 v10 and x10 But 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 0 .

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 1 through 9 instead of 01 through because [thereisnoambiguityInotherwordsifIhadattemptedtochange a10 v10 and x10 to az vz ] and xz ] and a replacement pattern of would have been fine, because the subsequent 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:

] Named BBEdit Replacement Pattern

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:

 BBEdit regex manual excerpt "title =" BBEdit regex manual excerpt "/> </p>
<p>  BBEdit is currently on Version 12 , 5; Version 6.5 came out in 2001. But the manual will ensure that long-term customers (I think it was on version 4 when I first bought it) do not get confused by changes in behavior, even when they changed almost two decades ago. [19659022] Previous post </p>
</div>
</pre>
</pre>
<script async src=
Source link