RegEx compilation thread

    Fellow Starmadian

    Oh cool so thats what this is
    Joined
    Jun 7, 2014
    Messages
    227
    Reaction score
    87
    • Community Content - Bronze 1
    • Wired for Logic
    • Legacy Citizen 2
    Since it doesn't seem like anyone else has done this yet, I'm going to start a thread to document all the practical uses in starmade for regEx that I've discovered. Hopefully this will allow people to use the the new features of display blocks without:
    A: Learning regEx.
    B: Depending on a player with a logic badge or equivalent knowledge to help them.

    This isn't to say that learning regEx is bad, as it's actually a good thing. It's not overly difficult either, I spent 4-5 hours studying the language and already I'm starting to understand most of the concepts. But I can totally agree with people that don't want to take the time to learn a new coding language. It's a fairly old and seldom used language. So seldom used in fact that I've found stack overflow pages with a single response to my googled question saying "regex isn't the best for this."

    So to prevent hair pulling and google crusades, here's a frequently updating list of all the practical applications and regex statements I've discovered. I'm sure these aren't nearly optimized, and I will update the different applications as more optimized methods are discovered. I use Regex101 - online regex editor and debugger to test my code in, it's much more useful than testing in starmade because it gives you feedback on what every part of the code does. Still have to test in starmade though because there are quirks that must be overcome.

    p.s. each line is a regex statement that needs to be in its own display block. To edit the contents of a display, put a button or activator next to the display you want to use as the modifier, then V the button to the display module you want to modify.
    ===============================================
    IMPORTANT: SPACES ARE HARD TO SEE, USE SEMICOLONS TO SEPARATE FIELDS. WHY IS THIS IMPORTANT? LOOK AT THIS CODE:
    PlayerPoints1[300] PlayerPoints2[400] PlayerPoints3[500] PlayerPoints4[700]
    huh, spaces, cool.
    ;PlayerPoints1[300];PlayerPoints2[400];PlayerPoints3[500];PlayerPoints4[700];
    woah, semicolons!!!!!!!
    I'm explaining this poorly, but take my word for it.. when you work with regex, it REALLY loves to grab whatever it can into it's precious matches. For compatibility reasons, it's better in the long run to use semicolons to replace spaces for different fields. then you can specify between strings and variables.
    ===============================================
    Jaaskinal- "If you're looking to add a command to the beginning of a text, I like to use [replacefirst][with][add] since it has no prerequisite for what characters must be contained in the text.

    GalactusX- "Currently the function [REPLACEFIRST] ... [WITH] and [REPLACEALL] ... [WITH] can not replace something, with nothing ... BUT We can tell the regex command to replace anything with an "empty field" using references"
    [ Text to replace all numbers "I play StarMade 0.199.429" --> [REPLACEALL][0-9.]()[WITH]$1 --> Result "I play StarMade" ]
    The parentheses are the blank field, the $num symbol copies the group with the matching number. The second (1st) match is nothing, so it replaces what is selected (the numbers) with nothing.
    For a long while off and on, I've been trying to create a basic scripting language in starmade, a way to convert sentences into a series of logic commands and have each command activate some logic. To do this, you need two different statements, one to remove the first field (word), and another to remove the last. This example uses : as the word separator, so you can have multiple words in a single field.
    EX: :test: of the: string parsing :system:
    1st statement: Regex101 - online regex editor and debugger
    2nd statement: Regex101 - online regex editor and debugger
    Code:
    removal of first field: (replace number with amount of fields forward you want)
    [replaceall]^(:([^:]*:){2})[with]:
    
    removal of last field (2 steps): (replace number with amount of fields back you want)
    [replaceall](:([^:]*:){2})$[with]:
    I'm sure there is a way to dynamically read each word to a quite extended length, but as of now I only know how to do it statically. for each word you know the sentence will have, you need to run those two bits of code until you have only 1 field left, then store that text. you can do this by making a display shift register.
    This doesn't work properly, I need to find a way to remove the line entirely instead of replacing the line with something else... It's the same as string parsing, but use these two sets of code instead:
    1st statement: Regex101 - online regex editor and debugger
    2nd statement: Regex101 - online regex editor and debugger
    Code:
    to erase the first line:
    ^(.*\n)
    
    to erase the last line:
    (\n.*)$
    Very simple to create with any amount of displays. To set one up, just daisy chain a row of buttons or activators, then place a display block adjacent to each button. At the end of the chain, connect the button to the display block of the next button down the line. Continue all the way back to the beginning of the chain. Put some text in the display block at the end of the chain and press the button at the beginning. Thanks to instant pulses, the buttons are activated one after the other down the chain! The second display updates the first, the third display updates the second, and so on.
    Snapems' logic counter:
    RegEx Logic - Counter
    I actually have yet to figure out how this works, so I'll leave the explaining to Snapems
    1st statement: Regex101 - online regex editor and debugger
    2nd statement: Regex101 - online regex editor and debugger
    (these work with any number, mess around on the website a bit and figure out how to substitute the number in the test string with a different number.
    Code:
    to count up a number in a display block:
    [REPLACEALL]\b([0-9]+)\b[WITH]0$1~01234567890
    [REPLACEALL]\b0(?!9*~)|([0-9])(?=9*~[0-9]*?\1([0-9]))|~[0-9]*[WITH]$2
    1st statement: Regex101 - online regex editor and debugger
    2nd statement: Regex101 - online regex editor and debugger
    3rd statement: Regex101 - online regex editor and debugger
    (doesnt seem to be neccesary)
    Code:
    to count down:
    [REPLACEALL]\b([0-9]+)\b[WITH]$1~09876543210
    [REPLACEALL]\b0|([0-9])(?=0*~[0-9]*?\1([0-9]))|~[0-9]*[WITH]$2
    (The third statement removes a potential leading 0 from the beginning instead of leaving it there.)
    [REPLACEALL]\b0|([0-9])(?=0*~[0-9]*?\1([0-9]))|~[0-9]*[WITH]$2
    =========================================
    Jaaskinal's hexidecimal counter:
    1st statement:Regex101 - online regex editor and debugger
    2nd statement:Regex101 - online regex editor and debugger

    1st statement:Regex101 - online regex editor and debugger
    2nd statement:Regex101 - online regex editor and debugger
    Code:
    To count up:
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~0123456789ABCDEF0
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2
    
    To count down:
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~0123456789ABCDEF0
    [REPLACEALL]\b0|([0-F])(?=0*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2
    
    To count up in odd numbers, but also skip odd beginnings:
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~013579BDF1
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2
    
    To count forwards but also backwards at the same time: (i guess)
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~0FEDCBA9876543210
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2
    ---------------------------------------------------------------------------------------
    Jaaskinal's binary counter:
    1st statement: Regex101 - online regex editor and debugger
    2nd statement: Regex101 - online regex editor and debugger

    1st statement: Regex101 - online regex editor and debugger
    2nd statement: Regex101 - online regex editor and debugger
    Code:
    To count up:
    [REPLACEALL]\b([0-1]+)\b[WITH]0$1~010
    [REPLACEALL]\b0(?!1*~)|([0-1])(?=1*~[0-1]*?\1([0-1]))|~[0-1]*[WITH]$2
    
    To count down:
    [REPLACEALL]\b([0-1]+)\b[WITH]0$1~010
    [REPLACEALL]\b0|([0-1])(?=0*~[0-1]*?\1([0-1]))|~[0-1]*[WITH]$2
    That's all for now, but I'm currently focusing on regex related logic so there's sure to be more soon!
    Here's the link to the breadboard demonstration for all the regex I'm working on:
    regEx compilation breadboard currently contains:
    -binary multiplier (binary adder with slight conversion, god binary multiplication is easy)
    -display decimal to logic binary converter. (uses the binary multiplier, can go back to a display block as binary easy, Im just lazy lol. I'll add it later..)
    -regex string field seperator (my string parser. seperates :fields: onto separate displays.)
    -regex variable extractor/editor/injector. (Used GalactusX's regex heavily as reference)[/URL]
    -Jaaskinal: [REPLACEFIRST](.*)(.)[WITH]$2$1
    -Jaaskinal: [REPLACEFIRST](.)(.*)[WITH]$2$1
    -lessons and such (flesh out plox)
    -fellow_starmadian: Regex101 - online regex editor and debugger
    -Regex101 - format tester 2nd
    -Regex101 - format tester
    -Regex101 - use this as base for v2 of variable pointer system
     
    Last edited:

    Fellow Starmadian

    Oh cool so thats what this is
    Joined
    Jun 7, 2014
    Messages
    227
    Reaction score
    87
    • Community Content - Bronze 1
    • Wired for Logic
    • Legacy Citizen 2
    I'll have to make a ship showing off all the different things discussed here every week or so, for even better understanding.
     
    • Like
    Reactions: Magrim

    Jaaskinal

    ¯\_(ツ)_/¯
    Joined
    Jan 19, 2014
    Messages
    1,377
    Reaction score
    646
    • Legacy Citizen 4
    • Wired for Logic Gold
    • Thinking Positive
    For my nasometer, I needed to increment in decimal and in hexidecimal, so I decided to figure out Snapems counter. TBH, modifying it is p. easy.

    As a base, the original is
    [REPLACEALL]\b([0-9]+)\b[WITH]0$1~01234567890
    [REPLACEALL]\b0(?!9*~)|([0-9])(?=9*~[0-9]*?\1([0-9]))|~[0-9]*[WITH]$2

    My modified Hexidecimal counter is
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~0123456789ABCDEF0
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2

    I simply replaced every instance of 9 with F, and added in some letters to get where I wanted to go. Just for gits and shiggles though, I wanted to play around and make a binary counter
    [REPLACEALL]\b([0-1]+)\b[WITH]0$1~010
    [REPLACEALL]\b0(?!1*~)|([0-1])(?=1*~[0-1]*?\1([0-1]))|~[0-1]*[WITH]$2

    Which indicates a few things, really. Every time I've listed 9, F, or 1, that's the maximum number, and then that string of numbers at the end is all the possible components, in order, and then the first number again. For more shiggles, I fucked around with that last string. This counts up in hexidecimal using only odd numbers, but also skips even number beginnings (F + 1 = 11, 1F + 1 = 31)
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~013579BDF1
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2

    This one just does weird shit I don't even understand
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~0FEDCBA9876543210
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2

    Anyways, all of this is just to say that modifying this piece of Regex is easy and can do powerful things.
     
    • Like
    Reactions: Fellow Starmadian

    Fellow Starmadian

    Oh cool so thats what this is
    Joined
    Jun 7, 2014
    Messages
    227
    Reaction score
    87
    • Community Content - Bronze 1
    • Wired for Logic
    • Legacy Citizen 2
    I'll make a counting category and add this along snapem's stuff :)

    I know this probably isn't possible, but I wonder if there's a way to convert from decimal to binary? I currently have a big binary adding and multiplying system that takes up a lot of space :/
     
    Joined
    Sep 9, 2013
    Messages
    68
    Reaction score
    10
    • Legacy Citizen 3
    And counting down is like:

    [REPLACEALL]\b([0-9]+)\b[WITH]$1~09876543210
    [REPLACEALL]\b0|([0-9])(?=0*~[0-9]*?\1([0-9]))|~[0-9]*[WITH]$2
    [REPLACEALL]\b0|([0-9])(?=0*~[0-9]*?\1([0-9]))|~[0-9]*[WITH]$2

    Note, step 3 is the same as step 2, so you can just repeat that step w/ your logic.
    Cool hex'o counters and jazz Jaaskinal

    Fellow Starmadian Regex is really not designed to do math operations. converting a number's base would be pretty insane if possible.
    The first solution to converting a number that comes to mind would be to count up in base X while you count down in base Y, until the Y number hits 0. (or in this case i think it turns blank).
    You might be able to count by larger integers than 1 to speed the process up. >_>. Difficult & weird, but possible!
     

    Fellow Starmadian

    Oh cool so thats what this is
    Joined
    Jun 7, 2014
    Messages
    227
    Reaction score
    87
    • Community Content - Bronze 1
    • Wired for Logic
    • Legacy Citizen 2
    added the counter to your project snapem :) Currently editing all of the entries to have all of their own www.regex101.com page.

    Snapems I don't believe the third statement for your decimal count-down regEx is actually required, it doesn't seem to change the string. You can look at the regex I added for your decimal counter entry.
     
    Last edited:

    Fellow Starmadian

    Oh cool so thats what this is
    Joined
    Jun 7, 2014
    Messages
    227
    Reaction score
    87
    • Community Content - Bronze 1
    • Wired for Logic
    • Legacy Citizen 2
    I've taken the liberty of filling in the down counters for the hexadecimal and binary entries. As Jaaskinal said, it's simple enough to modify these statements. I also added the counters you made for counting odd in hexadecimal, and the weird one you don't understand :P
     

    DukeofRealms

    Count Duku
    Joined
    Sep 4, 2013
    Messages
    1,476
    Reaction score
    1,616
    • Schine
    You can use [code=pcre] BBCode (Perl Compatible Regular Expressions) to get syntax highlighting.

    Code:
    To count up:
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~0123456789ABCDEF0
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2
    
    To count down:
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~0123456789ABCDEF0
    [REPLACEALL]\b0|([0-F])(?=0*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2
    
    To count up in odd numbers, but also skip odd beginnings:
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~013579BDF1
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2
    
    To count forwards but also backwards at the same time: (i guess)
    [REPLACEALL]\b([0-F]+)\b[WITH]0$1~0FEDCBA9876543210
    [REPLACEALL]\b0(?!F*~)|([0-F])(?=F*~[0-F]*?\1([0-F]))|~[0-F]*[WITH]$2
     
    • Like
    Reactions: nightrune
    Joined
    Jul 30, 2013
    Messages
    398
    Reaction score
    282
    • Wired for Logic Gold
    • Legacy Citizen 8
    • Purchased!
    I hope you can help me, I need a regex command, which will allow me to delete all the text from a line "except" the text that I need. for example

    Sample text: "I do not like regex commands, but it's useful"
    Text to search for: "regex"
    Final text: "regex"

    Basically I want to delete all the text that does not enter within the limits
     
    • Like
    Reactions: Stormraven
    Joined
    Jan 11, 2017
    Messages
    168
    Reaction score
    83
    I hope you can help me, I need a regex command, which will allow me to delete all the text from a line "except" the text that I need. for example

    Sample text: "I do not like regex commands, but it's useful"
    Text to search for: "regex"
    Final text: "regex"

    Basically I want to delete all the text that does not enter within the limits
    Seconded!
     

    Jaaskinal

    ¯\_(ツ)_/¯
    Joined
    Jan 19, 2014
    Messages
    1,377
    Reaction score
    646
    • Legacy Citizen 4
    • Wired for Logic Gold
    • Thinking Positive
    I hope you can help me, I need a regex command, which will allow me to delete all the text from a line "except" the text that I need. for example

    Sample text: "I do not like regex commands, but it's useful"
    Text to search for: "regex"
    Final text: "regex"

    Basically I want to delete all the text that does not enter within the limits
    1: Copy the original statement onto a second display block.
    2: Use "[REPLACEFIRST]regex[WITH]temp" on the second display block.
    3: Use a sensor to determine if they are the same.
    4a: If they are the same, the text did not contain "regex".
    4b: If they are not the same, the text contained the word "regex", output "regex" onto another display block.
     
    Joined
    Jul 30, 2013
    Messages
    398
    Reaction score
    282
    • Wired for Logic Gold
    • Legacy Citizen 8
    • Purchased!
    1: Copy the original statement onto a second display block.
    2: Use "[REPLACEFIRST]regex[WITH]temp" on the second display block.
    3: Use a sensor to determine if they are the same.
    4a: If they are the same, the text did not contain "regex".
    4b: If they are not the same, the text contained the word "regex", output "regex" onto another display block.
    But what I need is not to "compare" 2 display blocks and see if they are the same, what I need is to "catch" the text of a text string and delete the rest; thanks for the tip, but i alrready try that but is not what i need Y_Y.

    I need a regex command typed in one display block and extract only the text inside in other
     

    Jaaskinal

    ¯\_(ツ)_/¯
    Joined
    Jan 19, 2014
    Messages
    1,377
    Reaction score
    646
    • Legacy Citizen 4
    • Wired for Logic Gold
    • Thinking Positive
    But what I need is not to "compare" 2 display blocks and see if they are the same, what I need is to "catch" the text of a text string and delete the rest; thanks for the tip, but i alrready try that but is not what i need Y_Y.

    I need a regex command typed in one display block and extract only the text inside in other
    Is this not the functionality you are describing?
     
    Joined
    Jul 30, 2013
    Messages
    398
    Reaction score
    282
    • Wired for Logic Gold
    • Legacy Citizen 8
    • Purchased!
    More or less yess, but what happens when what you want to know is what there is for example between [...], and what's inside you do not know?

    I have created several regex commands to store information in an array, and I need to be able to access that information without knowing it before, for example:

    PlayerPoints1 [...300...], PlayerPoints2 [...500...], PlayerPoints3 [...700...] ...

    I can save and modify the information, but I need a way to access it without knowing it before, so you only need to "ask" what are between PlayerPoints2[ ... ], and then regex commands delete everything except 500
     

    Fellow Starmadian

    Oh cool so thats what this is
    Joined
    Jun 7, 2014
    Messages
    227
    Reaction score
    87
    • Community Content - Bronze 1
    • Wired for Logic
    • Legacy Citizen 2
    I can see you're asking for something more in your latest post, to quote you:
    Sample text: "I do not like regex commands, but it's useful"
    Text to search for: "regex"
    Final text: "regex"
    Jaaskinal did that perfectly, but now you want a dynamic searching and deleting regex statement. Can I assume you only want 1 field to remain after deleting the extra fluff? You also need to choose some way of defining what the region to remove is, because your examples of "I do not like regex commands, but it's useful" and "playerpoint[500]" are completely different. In the first we are looking for a specific word and returning true if the word exists in the string, in the second we could search for whatever is between the brackets or search for the portion of the string with numbers.

    I guess what I'm saying is you need to have a very specific thing you want regex to do, or it won't be able to be done like you want it to be. One thing to do would be to have a complex data verification system, but that is far too complex to be practical to be used in your case. So until you decide for sure what you need it to do, I can't build a proper regex solution :/

    I'll try and catch you on CBS if I can, to go over it in person :P