RegEx.: Fetched Group not working at replacement string;?



  • Hi all;

    I have created this Search definition:
    (?:^(?:(G71)|(G97( S\d.))|(M12)|(M22)|(G00( T\d.))|(M90)|G98 P147 D1|G75)(\R?))|F([\d.]+)
    And this replace def.:
    (?1G21$9G64 P11 R4 S85 D0.03 F0.2$9)(?4;M12$9)(?5;M22$9)(?8;Processed by Find_Replace Script v1.0$9$9)(?6M6$7$9M3$3$9)(?10F[$10*60])

    Data Text:

    G90
    G71
    G00 T2
    G97 S12000

    M12
    G00 X320.422 Y161.899 Z-10 F3000

    I expect this after running the script:

    ;Processed by Find_Replace Script v1.0

    G90
    G21
    G64 P11 R4 S85 D0.03 F0.2
    M6 T2
    M3 S12000

    ;M12
    G00 X320.422 Y161.899 Z-10 F[3000*60]

    Does somebody know why I dont get the S12000 saved in Group3? or maybe it does save but it certainly the replacement string (?6M6$7$9M3$3$9) does not process the $7 group call, which is: “S 12000”.

    Can someone please try it and let me know where I go wrong; Is this me doing something silly or is it a limitation within the Boost implementation of RegEx? Thanks in advance…

    Complementary explanation:
    Basically this is the idea i came up with to replace and do a line swap for these 2 strings of the original data text:

    G00 T2
    G97 S12000

    processed it should be:

    M6 T2
    M3 S12000

    Cheers,
    F



  • @Flores-Francisco said in RegEx.: Fetched Group not working at replacement string;?:

    I have created this Search definition:
    (?:^(?:(G71)|(G97( S\d.))|(M12)|(M22)|(G00( T\d.))|(M90)|G98 P147 D1|G75)(\R?))|F([\d.]+)
    And this replace def.:
    (?1G21$9G64 P11 R4 S85 D0.03 F0.2$9)(?4;M12$9)(?5;M22$9)(?8;Processed by Find_Replace Script v1.0$9$9)(?6M6$7$9M3$3$9)(?10F[$10*60])

    I don’t really think that anybody is going to take a look at such a complicated scenario when you have failed to properly convey your regexes.

    When putting something like this in a posting, either wrap it in backticks so that it appears like this or put it in a code block (using </> in the compose window toolbar) so it appears like:

    this
    


  • Hello @flores-francisco, @alan-kilborn and All,

    @flores-franscisco, before going further on, I need some clarifications :

    • For the line beginning with G97, the number which follows the letter S is :

      • Always an integer as S0, S23 or S123456789

      • An integer or decimal number as S0.62, S74, S11.0 or S32

      • Always a decimal number as S9.152, S0.12345, S792.098 or S1234.0

    • Idem for the line beginning with G00 and the number which follows the letter T

    • Idem for the line beginning with G00 and ending with the letter F and a number

    • Apparently, group 8 should write at beginning of the replacement text, the string ;Processed by Find_Replace Script v1.0 followed with two line-breaks. But this group exists only if the string M90 is found at beginning of lines. And unfortunately, NO string M90 is present in your input text !!??

    See you later,

    Best regards

    guy038



  • @guy038

    1. Integer, Always. With G97 beggining.
    2. Integer, Always. After S…
    3. Integer mostly, sometimes it has decimal points. After F…

    Sorry about M90. It is always present. I could not edit the post.

    thanks,
    F



  • @Alan-Kilborn said in RegEx.: Fetched Group not working at replacement string;?:

    ng to take a look at such a complicated scenario when you have failed to properly convey your regexes.
    When putting something like this in a posting, either wrap it in backticks so that it appears like this or put it in a code b

    Thanks. U are right about the visual aids.

    Sadly this forum only allows editing posts within 3 minutes of posting… crazy:0|



  • @Flores-Francisco

    You can always add a post, clarifying your data or other points.



  • MY PROBLEM IS VERY SIMPLE:

    WHY THE $3 CALL ON HERE (?6M6$7$9M3$3$9) NOT WORKING??



  • @Flores-Francisco

    MY PROBLEM IS VERY SIMPLE:

    Don’t shout (ALL CAPS IS CONSIDERED SHOUTING IN EVERY INTERNET FORUM SINCE THE 90s).

    If the problem was simple, you could figure it out. Since you cannot, it’s obviously more complicated than you think, or you aren’t explaining it very well.

    The $3 in the replacement would be “not working” if the third match group in the search expression isn’t matching what you think it should. But since you didn’t show us what you did get vs what you want to get, we’d have to guess, which is obviously frustrating you.

    If you want better help, provide better data examples, using the </> button on the forum toolbar, so that the text is marked as a black box like Alan showed, otherwise the forum mangles data. Follow the advice you read below.

    ----

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the </> toolbar button or manual Markdown syntax. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.



  • Hi, @flores-francisco, @alan-kilborn, @peterjones and All,

    You asked the community :

    Does somebody know why I dont get the S12000 saved in Group3? or maybe it does save but it certainly the replacement string (?6M6$7$9M3$3$9) does not process the $7 group call, which is: “S 12000”.

    For the simple reason that, when your regex matches G00 followed with T2 ( Groups 6 and 7 ), the regex cannot know the Group 3, at the same time, because Group 3 is known only if the regex matches the line G97 S12000 !!


    Now, I’ve completely changed the layout of your regex :

    • Firstly, I did not use any group ( $9 ) to capture the line-break I simply used the \r\n in the conditional replacements

    • Secondly, I re-organized the different alternatives, of the overall regex, in a more logical order

    • Thirdly, I merged the two similar cases M12 and M22

    • Fourthly, I omitted the G98 and G75 alternatives as NO conditional replacement exists for these cases

    • Fifthly, in the G00 and G97 lines, I used the regex \d+ to detect any integer and, in the last line, I used the regex F(\d+(\.\d+)?)$ to find the letter F, followed with a decimal or integer number, at the end of line

    Here is, below, my regex S/R with the search part written in the Free-spacing mode ( (?x) )

    The SEARCH regex is :
    
    (?x)
    (?:^              #  At START of line
     
      (M90)         | #  String "M90" ( Group 1 ) CHANGED as ";Processed by Find_Replace Script v1.0" + \r\n + \r\n + "M90"  =>  Replacement = (?1;........\r\n\r\nM$1)
    
      (G71)         | #  String "G71" ( Group 2 ) CHANGED as "G21" + \r\n + "G64" + " P11 R4 S85 D0.03 F0.2"  =>  replacement = (?2G21\r\nG64 P11 R4 S85 D0.03 F0.2)
    
      G00(\x20T\d+)
      \R
      G97(\x20S\d+) | # String "G00" + " T2" ( Group 3 ) + \r\n + "G97" + " S12000" ( Group 4 ) CHANGED as "M6" + " T2" + \r\n + "M3" + " S12000"  =>  Replacement = (?3M6$3\r\nM3$4)
    
      (M[12]2)        # String "M12" or "M22" ( Group 5 ) CHANGED as ";M12" or ";M22"  =>  Replacement = (?5;\5)
    
    )  |              # OR, at END of line...
    
    F(\d+(\.\d+)?)$   # String "F3000" ( Group 6 ) CHANGED as "F[" + "3000" + "*60]"  => Replacement = (?6F[$6*60])
    
    
    And the REPLACEMENT regex is :
    
    (?1;Processed by Find_Replace Script v1.0\r\n\r\n$1)(?2G21\r\nG64 P11 R4 S85 D0.03 F0.2)(?3M6$3\r\nM3$4)(?5;\5)(?6F[$6*60])
    
    
    Decomposition JUST for information :
    
    (?1;Processed by Find_Replace Script v1.0\r\n\r\n$1)
    (?2G21\r\nG64 P11 R4 S85 D0.03 F0.2)
    (?3M6$3\r\nM3$4)
    (?5;\5)
    (?6F[$6*60])
    
    
    • Select all text between (?x) and => Replacement = (?6F[$6*60])

    • Use the Ctrl + H shortcut to open the Replace dialog => The Find what: contains the search regex

    • Then, insert (?1;Processed by Find_Replace Script v1.0\r\n\r\n$1)(?2G21\r\nG64 P11 R4 S85 D0.03 F0.2)(?3M6$3\r\nM3$4)(?5;\5)(?6F[$6*60]) in the Replace with: box


    Without the free-spacing mode , my solution is :

    SEARCH (?:^(M90)|(G71)|G00(\x20T\d+)\RG97(\x20S\d+)|(M[12]2))|F(\d+(\.\d+)?)$

    REPLACE (?1;Processed by Find_Replace Script v1.0\r\n\r\n$1)(?2G21\r\nG64 P11 R4 S85 D0.03 F0.2)(?3M6$3\r\nM3$4)(?5;\5)(?6F[$6*60])

    So, assuming your initial text, below :

    M90
    G71
    G00 T2
    G97 S12000
    M12
    G00 X320.422 Y161.899 Z-10 F3000
    

    You’ll get your expected text :

    ;Processed by Find_Replace Script v1.0
    
    M90
    G21
    G64 P11 R4 S85 D0.03 F0.2
    M6 T2
    M3 S12000
    ;M12
    G00 X320.422 Y161.899 Z-10 F[3000*60]
    

    Best Regards,

    guy038



  • @guy038 said in RegEx.: Fetched Group not working at replacement string;?:

    Best Regards,
    guy038

    Guy I like how you see things.

    Thank you for your time good sir…

    I understand your explanation well.

    Regards…


Log in to reply