use regex to change number sets



  • Hello
    i have some kcode kniting machine code that i have been looking at and would like to ask some of your regex gurus
    were i might start or if its possible

    STIF:888888

    i would like to search this line of code in my file and change the all the no 8 to a list of values
    1-9 or A-F
    changed code
    stif:9999999

    the line has 152 numbers in it after STIF: parameter in the file
    is this something which could be done with the regex search and replace
    i have been doing by hand with the search and replace terribly slow and tedious as some files have 2-300 lines of this types of text in my files

    or is this something that could be done with a macro
    any input would be great as im not very good at the coding
    thanks



  • @junkdog

    For me at least, that description doesn’t tell me anything that I can use to help. Why don’t you show some “before” and “after” text examples to augment that description?



  • before code

    STRI:444444444

    after code

    STRI:5555555



  • @junkdog

    Again, it doesn’t make total sense.
    You need to give us enough before-and-after data for us to derive the pattern.
    I’m 100% sure it makes sense to you. But we can’t see what you see.

    As an example here of what doesn’t make sense:

    In your original example, you show six 8 characters being transformed into seven 9 characters.
    The second example shows nine 4 characters being changed to seven 5 characters.

    Maybe the pattern is that you always want seven characters in the result?
    This is just a total guess, because I think if it were true you’d have said “there is to always be seven characters in what I want to end up with”

    So, in the event that what I’ve surmised is true, I would suggest this regular expression replacement (which would work for your second-posting data):

    find: STRI:\d+
    repl: STRI:5555555

    where you could vary the 5 in the “repl” expression as you need.

    If that’s not what you need, please let us know.

    But, it is rather frustrating, when trying to extract the real problem is more difficult than providing a solution! :-)



  • @Alan-Kilborn i will post some of the code that is in my file

    one line of the code is

    STIF:888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

    there is 152 no 8 digits

    it is in one line in my file
    i was hoping to use the STIR: as a marker of sorts so that the search could find the numbers
    then change the numbers
    essentially change the entire row beyond the STIR:

    sorry for the bad descriptions of what im trying to do
    thanks for your time



  • @junkdog said in use regex to change number sets:

    there is 152 no 8 digits

    The find string (using regex) to look for this is:
    ^STIF:8{152}$
    provided the STIF is against the left margin and that the line ends with the last 8 character.
    However it would seem that you want to replace lines like this with different replacement strings depending on some criteria which you’ve yet to explain.

    You can use the above to bookmark the lines by ticking the “bookmark lines” when using the “Mark” function, make sure search mode is set to regular expression. Then you would use the “F2” (or “Shift-F2”, these are the default) hotkeys to go forward to next (or back to previous) bookmarked line.

    As a suggestion pick about 5-10 lines containing at least 2 (if not more) lines with the STIF lines to be edited in them, along with some other lines. Then insert them into a post in this thread. Then select all those example lines and click on the </> button you will see above the posting window. That will encapsulate the examples in a black window so we can see the data in it’s natural state (the posting window has a tendency to alter some of the characters as it thinks it is part of the formatting code), this prevents that occurring.

    Then duplicate the examples in the post but with the changes made so we can more easily see what your intention is.

    Terry



  • @junkdog said in use regex to change number sets:

    as a marker of sorts so that the search could find the numbers

    1. Use regex to bookmark the lines
      Use the Mark function with the regex ^STIF:8{152}$
      Click “Mark All”

    2. Select the main tab with the file contents and click F2 to select the next bookmarked line. Use Shift-End-End which will highlight/select that entire line’s contents (if you use word wrap the 2nd End is needed)

    3. Use the Replace function to change the 8 to which ever character required for that line. Make sure the “in selection” box is ticked. This will be a one to one replacement, so as there are 152 “8” characters (if using my bookmark regex), there will be 152 replacements, which you can verify with the bottom status bar of the Replace function. Find What is 8, the Replace With field is which ever character you want as replacement (you referred to 1-9 or A-F). Click on the “Replace All” button, after making sure the “in selection” box is ticked.

    Repeat steps 2 and 3 until you have updated all bookmarked lines. You will know that once the next bookmarked line selected (F2) does NOT have any “8” characters.

    Until you come back with additional information that’s about all I can offer up.

    Terry



  • Hello Terry

    i tried using the bookmarks and find and replace it seems to work great
    thanks for that
    if my understanding is correct this is what this means

    ^STIF:8{152}$
    

    the caret ^ acts as anchor for the front of the line
    the STIF: is the line refered to
    8 is what were looking for
    inside the brackets is the 152 times were looking for the number “8”
    $ is the end of the line

    could i just use the “+” to make it greedy to the end of the line to grab all of the number"8"

    ^STIF:8+$
    

    i tried it it works as well
    thanks for the code i had really no idea were to start just an old dumb welder trying to learn this stuff to help out my wife with her new machine
    now this whole syntax thing makes a lot more sense after a bunch of reading and messing about
    i will post some more code
    this is from a program for kcode a text file very simular to gcode for cnc machines
    but the kcode is for a knitting machine so the numbers in this case represent a stitch size
    with the parameter of which needle bed to use "STIF:’ stitch front or “STIR:” stitch rear
    then there is "0-9’ and A-F as stitch types
    eventually would like to put it into a macro or some kind of script to be able to change those stitch types in the selected area in the file easily

    this i basically two passes of the machine

    // row: 0
    // racking: 0.0
    // reasons: PositionCarriage, Kickback, Active, Coalesced, 
    FRNT:......3........._____________________________________________________________________________________________________-_-_-_-_-_-_-_-_-_-____3_\_______________________________________________________________________________________________________________________________...............
    A-REAR:......3.........______________________________________________________________________________________________________-_-_-_-_-_-_-_-_-_-___3_\_______________________________________________________________________________________________________________________________...............
    STIR:444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
    >> Kn-Kn 0 500 440 0
    //
    //
    // row: 1
    // racking: 0.0
    // reasons: Active, PositionCarriage, Coalesced, 
    FRNT:../.............___________________________________________________________________________________________________3___-_-_-_-_-_-_-_-_-_-___3_\_______________________________________________________________________________________________________________________________...............
    STIF:888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
    REAR:../.............___________________________________________________________________________________________________3__-_-_-_-_-_-_-_-_-_-____3_\_______________________________________________________________________________________________________________________________...............
    STIR:444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
    << Kn-Kn 0 500 440 0
    

    thanks again for your time



  • @junkdog said in use regex to change number sets:

    i had really no idea were to start just an old dumb welder trying to learn this stuff to help out my wife with her new machine

    On the contrary, you are doing well so far. Your adjusted version using the + will do okay so long as you accept that if any other similar lines of different length exist they will also get bookmarked. As you focused on 152 characters my regex did the same.

    What you are yet to explain is what criteria you use to replace the 8 with another number or letter. Or maybe the whole file has those lines ALL replaced with the same character. And then at a different time the same file is again edited and the 8 replaced with yet another character.

    Your first post suggested that within the file some of these lines might have the 8 replaced with say A, and some others maybe replaced with say the 4, possibly even some of the rest replaced with a 2 , and so on.

    If all lines containing the 8 have it replaced with JUST 1 new character then yes a regex or macro could do that. In fact you could generate a macro for each of the replacement characters. What can’t be done is for the macro to ask you for the replacement character before processing, that requires the use of a programming language such as Pythonscript which is supported in Notepad++.

    Terry



  • @Terry-R said in use regex to change number sets:

    What you are yet to explain is what criteria you use to replace the 8 with another number or letter. Or maybe the whole file has those lines ALL replaced with the same character. And then at a different time the same file is again edited and the 8 replaced with yet another character.
    Your first post suggested that within the file some of these lines might have the 8 replaced with say A, and some others maybe replaced with say the 4, possibly even some of the rest replaced with a 2 , and so on.

    yes you are correct i wish to replace the 8 with another number or letter in only selected parts of the code as each row as it were is one travel of the carriage of the machine

    the selection of the row as it were might be 1 row or 20 depending on what the machine is doing

    // row: 3
    // racking: 0.0
    // reasons: Active, PositionCarriage, Coalesced, 
    FRNT:../............._________________________________________________________________________________________________2_____-_-_-_-_-_-_-_-_-_-___2_\_______________________________________________________________________________________________________________________________...............
    STIF:444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
    REAR:../............._________________________________________________________________________________________________2____-_-_-_-_-_-_-_-_-_-____2_\_______________________________________________________________________________________________________________________________...............
    STIR:444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444
    << Kn-Kn 0 500 440 0
    

    that is one row of stitches across the machine
    so changing the no “8” is actually changing the stitch size for a portion of the garment

    is there a way to in the replace part of the equation to remove the “STIF:” part and only deal with the digits maybe some grouping or some other thing

    how did you get the items that you were highlighting in the forum to turn red its less confusing the
    " " symbols

    is python script the language of choice for the notepad++ and how hard is to integrate into program or is that something i would maybe find someone to do for me and just pay them some bucks i do a lot of solidworks and machine programming for my cnc equip simple macros and
    editing some machine code by hand mostly vba which is copied and pasted modified to suit my needs but writing a script to integrate into a program i haven’t used much would be an interesting project but probably use up a lot of my time

    thanks again



  • @junkdog said in use regex to change number sets:

    in only selected parts of the code

    Unless you can programmatically identify which parts of the file need adjustment it will likely be a challenge to use a macro or even look at Pythonscript as a solution.

    I do note that the example code you provided does seem to allow for comment lines. Perhaps you could add comment lines before each STIF or STIR line with some sort of code which would allow for a macro to select ONLY those lines you wish to work on.

    Your question about the replacement option is a good one. AFAIK there is no way to say "write 152 9" as say 9{152}. Each replacement character inserted must be typed unless it already exists in the selected line and can be referenced as one of the capture groups.

    My regex used the STIF to limit lines selected. If the replacement field was used it would ALSO need to reference those characters, perhaps as a capture group, so it is written back otherwise it would be lost. There is an exception to that, by using the \K but this has a limitation in that only the Replace All can be used. Not normally an issue, but I prefer not to use it if at all possible. But I digress, these are all deep conversation pieces which possibly have no place here currently. This is an advanced regex function.

    As for me supplying regex code in red, that is achieved by using the backtick character. If you are aware of the normal single and double quotes, think of the backtick as being a mirror image of the single quote. On most English keyboards it co-exists on the same key as the tilde ~, top left of the number keys. Place one either side of the text to be shown in red.

    Pythonscript is possibly the best supported and used programming languages in Notdpad++. If you haven’t done any programming (copying and editing existing code is a good start but not the same as creating original code) this would be a good one to learn, but it won’t be a quick result.

    To make or have someone supply a solution every step must be able to be done programmatically. I’m still not certain your criteria for whether to edit a particular line or not can currently be achieved programmatically.

    Terry



  • Terry

    thanks very much for the insight
    i will talk to the developer of the software that is used to develop the code it is still in early stages
    maybe they can incorporate some of this into the front end

    as it were as best i can describe it the program is a visual pixel by pixel generation of the artwork or knitted garment then it is compiled into the code with a compiler which creates the kcode that we were looking at
    at this moment there is no way to change the stitch size or a bunch of other parameters easily
    other than re compiling the original file with changes which are line by line
    i was just hoping without to much extra credits i could figure a way to easily edit the back end

    i have used in the past some post editing software for my cnc files which allow you to edit the back end the gcode as it were which is very simular to the k code in some aspect
    these editors were great you can adjust many actual parameters to the gcode very easily
    but this type of stuff ahs been around for a long time

    this allows you base level flexibilty if you know what your doing
    gcode is developed from cad file into a machining program ie mastercam which has a compiler with presets for your machine which outputs the gcode to move the machine around

    your idea of the bookmarks has been working great and thanks for the tipabout the red color it makes it easier to not screw it up

    thanks again for your time
    hope thing are well with you and your family in these times
    have a good weekend


Log in to reply