I spend some time trying to do different calculation and similar in RegEx as compact as possible and this is the stuff I came up with so far. It seems like the most complex thing that was around before was counting up and down, so this should be quite helpful for building stuff without lots of logic blocks.
I will probably add more stuff later and you can also ask me if you want something specific of this kind.
Also most of this isn't well tested. I am quite confident that it works but please tell me if it doesn't.
All commands (so far) require the input the be formatted in a certain way. I don't provide the code to get your inputs into this format, but that's really not the hard part and I hope you can figure that out on your own.
LENGTH needs to be replaced with the length of the numbers you want to add. INPUT_A and INPUT_B are the numbers you want to add, which need to be the length specified with LENGTH. When the inputs are shorter, just add 0's to the beginning. The stuff behind the '~' is basically the truth table of a full adder.
The command only need to be run once and its finished.
Example: (108+84=192 / 01101100+01010100=11000000)
Regex101 - online regex editor and debugger
LENGTH needs to be replaced with the length of the numbers you want to add. INPUT_A and INPUT_B are the numbers you want to add, which need to be the length specified with LENGTH. When the inputs are shorter, just add 0's to the beginning. The stuff behind the '~' is the lookup table for adding the digits, the main problem of this was to actually fit all information in the 240 character limit.
The Addition is done in with two commands, each command is only run once. The second command just takes the output of the first without additional formatting.
Example: (382829+239091=621920)
Step 1: Regex101 - online regex editor and debugger
Step 2: Regex101 - online regex editor and debugger
INPUT is the number you want to convert. A length does not need to be specified in the command itself.
Command 1 needs to be run once per digit (if you run it more or less often the output is wrong) in the input and converts the binary number to a BCD number. Command 2 then takes the output of Command 1 and converts it to decimal. It only needs to be run once.
Example: (10100010 = 162)
Command 1: Regex101 - online regex editor and debugger
Command 2: Regex101 - online regex editor and debugger
INPUT is the number you want to convert. A length does not need to be specified in the command itself.
Command 1 needs to be run once. Command 2 needs to be run once for each digit you want in the output. Command 3 needs to be run once.
Example: (162 = 10100010)
Command 1: Regex101 - online regex editor and debugger
Command 2: Regex101 - online regex editor and debugger
Command 3: Regex101 - online regex editor and debugger
I will probably add more stuff later and you can also ask me if you want something specific of this kind.
Also most of this isn't well tested. I am quite confident that it works but please tell me if it doesn't.
All commands (so far) require the input the be formatted in a certain way. I don't provide the code to get your inputs into this format, but that's really not the hard part and I hope you can figure that out on your own.
Code:
Formatting:
INPUT_A:INPUT_B~01000_0001_1010_1100_1011_0110_0101_0111_1
Command:
[REPLACEALL](\d)(?=\d*:)(?=.{LENGTH}(.))((?=(1|.(?=.{LENGTH}1))*?1.{LENGTH}1\d*~.(1))|(?=.*?~(0)))(?=.*?~.*?\1\2(\5|\6)_(.))|:.*[WITH]$8
The command only need to be run once and its finished.
Example: (108+84=192 / 01101100+01010100=11000000)
Regex101 - online regex editor and debugger
Code:
Formatting:
INPUT_A:INPUT_B~0;0_01_12_23_34_45_56_67_78_89_9!1;1_22_33_44_55_66_77_88_9!9_02;2_43_54_65_76_87_9!8_09_13;3_64_75_86_9!7_08_19_24;4_85_9!6_07_18_29_35;5_06_17_28_39_46;6_27_38_49_57;7_48_59_68;8_69_79;9_8~!0;1~~01234567890
Command 1 (Digit Addition):
[REPLACEALL](\d)(?=\d*:)(?=.{LENGTH}(\d)\d*~.*?(\1;.*?\2_(\d)|\2;.*?\1_(\d)))|(\d)(?=\d*~)(?<=(\d)..{LENGTH})(?=\d*~.*?(\6;.*?\7_.*?(!|;)|\7;.*?\6_.*?(!|;)))(?=\d*~.*~.*?(\9(.)|\10(.)))|~.*?~.*?~[WITH]$4$5$12$13
Command 2 (Carry):
[REPLACEALL](\d)(?=9*.[\d:]{LENGTH}1\d*~\d*?\1(\d))|:.*[WITH]$2
The Addition is done in with two commands, each command is only run once. The second command just takes the output of the first without additional formatting.
Example: (382829+239091=621920)
Step 1: Regex101 - online regex editor and debugger
Step 2: Regex101 - online regex editor and debugger
Code:
Formatting:
#:INPUT~;0101_1000;0110_1001;0111_1010;1000_1011;1001_1100;1010_1101;1011_1110;1100_1111,~0000_00001_10010_20011_30100_40101_50110_60111_71000_81001_9
Command 1 (Binary to BCD):
[REPLACEALL](,)?(\d)(\d*)(?=(?=,|:).*?~.*?;0*\2\3_(.)(.{3}))|(,)(\d)(\d{3})(?=(?=,|:))|(:)(\d)|(#)(?=\d)(?=.*(,))[WITH]$4$7$1$6$5$8$10$9$11$12
Command 2 (BCD to Decimal):
[REPLACEALL](\d+)(?=(,|:).*?~.*?~.*?\1_(.))|#|,|:.*[WITH]$3
Command 1 needs to be run once per digit (if you run it more or less often the output is wrong) in the input and converts the binary number to a BCD number. Command 2 then takes the output of Command 1 and converts it to decimal. It only needs to be run once.
Example: (10100010 = 162)
Command 1: Regex101 - online regex editor and debugger
Command 2: Regex101 - online regex editor and debugger
Code:
Formatting:
#INPUT:~;0101_1000;0110_1001;0111_1010;1000_1011;1001_1100;1010_1101;1011_1110;1100_1111,~0_00001_00012_00103_00114_01005_01016_01107_01118_10009_1001,
Command 1 (Decimal to BCDl):
[REPLACEALL](\d)(?=.*:.*~.*~.*?\1_(.{4}).*?(,))[WITH]$2$3
Command 2 (BCD to Binary):
[REPLACEALL](\d)(,)(\d{3})(?=(?=.*?:).*?~.*?;0*\1\3_(.)(.{3}))|(\d)(,)(\d{3})(?=.*?:)|(\d)(:)|(#),
[WITH]$2$7$4$6$5$8$10$9$11
Command 3 (Remove Formatting):
[REPLACEALL]#().*:|~().*[WITH]$1$2
Command 1 needs to be run once. Command 2 needs to be run once for each digit you want in the output. Command 3 needs to be run once.
Example: (162 = 10100010)
Command 1: Regex101 - online regex editor and debugger
Command 2: Regex101 - online regex editor and debugger
Command 3: Regex101 - online regex editor and debugger
Last edited: