RegEx Calculation Dump

    Joined
    Feb 24, 2017
    Messages
    2
    Reaction score
    1
    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.

    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
    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
    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
    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
    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
    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
    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
    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
     
    Last edited:
    • Like
    Reactions: Olxinos
    Joined
    Jan 11, 2017
    Messages
    168
    Reaction score
    83
    You could use the same fuzzy logic towers I have in my HHI Logic Core Module to sort out a number from a sentence, regardless of formatting. If you wanted to do nothing but ensure the number reads correctly, you could even chop the tower down to a single column of 3-6 blocks. (Depending on whether or not you want to plug the function into another logic computer.)
    [doublepost=1488900978,1488900926][/doublepost]I must admit, I have never used binary. What would be a practical application of this binary addition?
     
    Joined
    Feb 24, 2017
    Messages
    2
    Reaction score
    1
    Sorry for taking so long to respond, I had some problems with my computer. I should be more active in the next days.

    I might look at your stuff and how to get the inputs in format in the best way at some point but currently I concentrate on the calculations themselves.

    Binary addition would mostly be good when you want to add binary signals, which can be rather easily converted to binary strings and back, saving space and performance a full adder made from logic blocks would take. Also some other actions (like multiplying) don't seem to be doable well in decimal, so you might want to use binary in stead of decimal in more complex systems.

    Also one thing I was asking myself: When I have something new, should I edit my original post or make a new comment? I'm not sure what would be better.

    Next I'll probably do binary multiplication.
     

    DrTarDIS

    Eldrich Timelord
    Joined
    Jan 16, 2014
    Messages
    1,114
    Reaction score
    310
    You could use the same fuzzy logic towers I have in my HHI Logic Core Module to sort out a number from a sentence, regardless of formatting. If you wanted to do nothing but ensure the number reads correctly, you could even chop the tower down to a single column of 3-6 blocks. (Depending on whether or not you want to plug the function into another logic computer.)
    [doublepost=1488900978,1488900926][/doublepost]I must admit, I have never used binary. What would be a practical application of this binary addition?
    I can see binary being a thing if you use sensor states to write to a specific place in a storage block. could be a good way to run certain logics.
    Like calculating weather or not your passive effects are "on" based on power reading or some such, or semi-intelligent/scripted rail/jumpdrive/interdiction states.