It is necessary in Visual Studio in the lines of the form
smth 456 reduce all numbers by 24.
I want to do this using search and replace by regular expression.
It is necessary in Visual Studio in the lines of the form
smth 456 reduce all numbers by 24.
I want to do this using search and replace by regular expression.
For some unknown reason, VS does not work this solution :(
You can subtract one 24 times from the number. For this:
~012345678!95(?=~.*(4)) , and for zero 0(?=~.*(!9)) on the group to be captured (we combine the regulars through | , we simply concatenate the groups).0!(?=9.+(!9))|1!(?=9.+(0))|... - we repeat the replacement until there is something to replace.~012345678!9Here is the code for generating regulars and checking the correctness of such a replacement:
var reg1 = RegExp(Array(10).fill().map((x,i)=>`${i}(?=~.*(${i?i-1:'!9'}))`).join("|")) var reg2 = RegExp(Array(10).fill().map((x,i)=>`${i}!(?=9.+(${i?i-1:'!9'}))`).join("|")) var repl = Array(10).fill().map((x,i)=>"$"+(i+1)).join("") var tail = "~012345678!9" console.log(reg1) console.log(reg2) console.log(repl) console.log(tail) function sub24(s) { var s = s.replace(/$/, tail) for (var q=0; q<24; ++q) { s = s.replace(reg1, repl) for (var next; (next = s.replace(reg2, repl)) !== s; s = next); } return s.replace(tail, "") } for (var x=25; x<2000; ++x) { if (sub24(x+"") != x-24) { console.error(x) } } console.log("Done!") .as-console-wrapper.as-console-wrapper { max-height: 100vh } Well, since 24 is a lot, you can add one point and make 2 + 4 = 6 replacement cycles:
var reg01 = RegExp(Array(10).fill().map((x,i)=>`${i}(?=~.*(${i?i-1:'!9'}))`).join("|")) var reg10 = RegExp(Array(10).fill().map((x,i)=>`${i}(?=.~.*(${i?i-1:'!9'}))`).join("|")) var reg2 = RegExp(Array(10).fill().map((x,i)=>`${i}!(?=9.+(${i?i-1:'!9'}))`).join("|")) var repl = Array(10).fill().map((x,i)=>"$"+(i+1)).join("") var tail = "~012345678!9" console.log(reg01) console.log(reg10) console.log(reg2) console.log(repl) console.log(tail) function sub24(s) { debugger var s = s.replace(/$/, tail) for (var q=0; q<2; ++q) { s = s.replace(reg10, repl) for (var next; (next = s.replace(reg2, repl)) !== s; s = next); } for (var q=0; q<4; ++q) { s = s.replace(reg01, repl) for (var next; (next = s.replace(reg2, repl)) !== s; s = next); } return s.replace(tail, "") } for (var x=25; x<2000; ++x) { if (sub24(x+"") != x-24) { console.error(x) } } console.log("Done!") .as-console-wrapper.as-console-wrapper { max-height: 100vh } This answer is similar to the next one , but it uses simpler regulars (and, with a different tail) and works in Visual Studio.
About the scheme itself:
To reduce the number by 1, if it does not end at 0, it is necessary to reduce its last digit to the previous one. Since there are no such combinations in regular expressions, you need to find it somewhere. Therefore, you can add a tail ~9876543210 to the number. Then on the regular basis (\d)(~\d*\1(\d)) we will have a replaceable digit of $1 , a reduced digit of $3 , and a tail that we don’t want to change to $2 . It turns out the replacement of $3 .
But there is a problem with zero reduction. We would like a nine after it, but you have to reduce the previous figure as well. Mark this with an exclamation mark. We change the tail for ~9876543210!9 , and in the regular season we add the ability to choose it: (\d)(~\d*\1(!?\d)) . Now for numbers 1-9 everything remains the same, and 0 turns into !9 .
In the latter case, we need to make a transfer. We are already almost able to subtract a unit, so we are doing the same, only in the number we are guided not by ~ , denoting the end of the number, but by ! indicating the need for transfer. The exclamation mark itself must be thrown out, but if another transfer is required, then we will have a new one, so this replacement must be repeated until it is applicable. And here is the modified regular schedule: (\d)!(9\d*~\d*\1(!?\d)) is the exclamation mark in front of the nine and optional numbers between it and the tilde, which marks the end of the number. Replacing the same.
So we learned to subtract 1 . To subtract 10 or 100, you just need to specify the number of unchanged numbers in front of the tilde in the first regular season:
1: (\d)(~\d*\1(!?\d)) 10: (\d)(\d~\d*\1(!?\d)) 100: (\d)(\d\d~\d*\1(!?\d)) Regular transfer can already do the transfer from anywhere:
(\d)!(9\d*~\d*\1(!?\d)) All replacements are made on
$3$2 Well, do not forget to then remove the added tail itself ~9876543210!9 .
Leading zeros may remain - they should also be deleted.
You can do this by simultaneously replacing 0*(\d)+~9876543210!9 with $1 .
And here is the code to verify subtraction 24:
var reg01 = /(\d)(~\d*\1(!?\d))/ var reg10 = /(\d)(\d~\d*\1(!?\d))/ var reg2 = /(\d)!(9\d*~\d*\1(!?\d))/ var repl = "$3$2" var tail = "~9876543210!9" var last = /0*(\d+)~9876543210!9/ function sub24(s) { var s = s.replace(/$/, tail) for (var q=0; q<2; ++q) { s = s.replace(reg10, repl) for (var next; (next = s.replace(reg2, repl)) !== s; s = next); } for (var q=0; q<4; ++q) { s = s.replace(reg01, repl) for (var next; (next = s.replace(reg2, repl)) !== s; s = next); } return s.replace(last, "$1") } for (var x=25; x<2000; ++x) { if (sub24(x+"") !== "" + (x-24)) { console.error(x) } } console.log("Done!") Source: https://ru.stackoverflow.com/questions/791323/
All Articles