Jump to content

Is dynamic SERC StringRegExpReplace possible please?

Recommended Posts

Hi folks, 

Thank you so much in advance for your help!  I've been using AUTOIT for manipulating gcode.  So far I've just worked through the excellent help examples and although I'm sure the resulting code is clumsy it has functioned :) 

However now I'm trying to improve and advance things and I've stumbled across REGEX.. and I'm a bit stuck.  What I would like to be able to do is to 'move'/'transform' the gcode in a file and re-write it to a new file.  I only need to move it in one direction(X).  At the heart of this I need a script to extract all the X values and then ADD or SUBTRACT an adjustment factor to transform and rewrite the code accordingly.

So far using an example script and an example input -

Func Test2()

Local $iMove = -4
    Local $sInput = '"G1 X45.036 Y6.934 F7800.000 G1 Z0.600 F7800.000 G1 F900 G1 X48.036 Y1.076 E0.58925"'
    Local $sOutput = StringRegExpReplace($sInput, '(?<=[X])\d+.\d+', '\0')
    Display($sInput, $sOutput)
EndFunc   ;==>Test2

This identifies the correct values i.e 45.036 and 48.036 but is there a way to dyamically adjust them before they are replaced, by for example a factor of -4 ($iMove above).  So far I can't seem to do math on the '\0' value i.e '\0'+ -4 ?

Many thanks for your time and expertise!


Link to post
Share on other sites

Thank you both for your help.. Wow it works, although I don't really understand the execute line and all the brackets etc..

I had seen Execute() referred to in some of the scripts but there's almost nothing on it in the Help of Autoit so I'll have to get to know it.

Now I just need to build it into the rest of my setup.   Best wishes to you both :) 

Link to post
Share on other sites
22 hours ago, ni3dprint said:

although I don't really understand the execute line and all the brackets etc..

Sorry for the lack of explanations  :>

You can't use a function (in this case, Execute an operation) directly inside the "replace" part of a SRER
So the concept is to build a new string from the subject string by replacing each match by a string including the function, and then apply Execute on this new string

Local $iMove = -4
$old = "G1 X45.036 Y6.934 F7800.000 G1 Z0.600 F7800.000 G1 F900 G1 X48.036 Y1.076 E0.58925"

$string = "'" & StringRegExpReplace($old, "(?<=X)\d+.\d+", "' & Execute('$0+' & $iMove) & '") & "'"
Msgbox(0,"new string", $string)

$new = Execute($string)
Msgbox(0,"result", $old & @crlf & $new)

Hmm was it clear ?  :huh2:


Link to post
Share on other sites
  • Moderators



Hmm was it clear ?

Perfectly - and a nice piece of lateral thinking to get there. Bravo!


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Link to post
Share on other sites
  • 2 months later...

Thanks Mikell for your help a while back with this... all pretty mind blowing stuff :)

Now I have another slight issue - because the values have 3 or so decimal places when I apply the calculation I'm getting 10+ decimal places in the output.  How can I reduce the result to 3 decimal places please?  I've tried inserting Round into the script above in several different ways but no joy so far.  I wonder is there a way to set a variable for the entire script that keeps the precision of all calculations to 3 decimal places?





Link to post
Share on other sites

Hi Folks,

I may have solved it myself at least in a clumsy way - it looks like it may just be the old 'floating point arithmetic' issue?

Local $new = Execute("'" & StringRegExpReplace($original, "(?<=X)-?\d+\.\d+", "' & Execute('$0' & $or & $xmin) & '") & "'")
$Final = Execute("'" & StringRegExpReplace($new, "(?<=Y)-?\d+\.\d+", "' & Execute('$0' & $or & $ymin) & '") & "'")
$Finally = Execute("'" & StringRegExpReplace($Final, "(?<=[X,Y])-?\d+\.\d+", "' & Execute(Round('$0',3)) & '") & "'")


Link to post
Share on other sites

Thanks Mikell, thats a bit tidier than mine although it did the job :)

incidentally in the regex above -   (?<=X)-?\d+\.\d+ 

this will pick up any digits after an X and with a decimal, however if I want it to include digits with no decimal can this be done?

i.e it will pick up X0.0 but it won't find X0?

I did try but gave up and ended up just adding .0 to all single digits ;)


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jmp
      i am trying to get number from string using this code :
      #include <IE.au3> $oIE = _IEAttach ("Edu.corner") Local $aName = "Student name & Code:", $iaName = "0" Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.InnerText = $aName Then $iaName = $oTd.NextElementSibling.InnerText $iGet = StringRegExpReplace($iaName, "\D", "") EndIf Next MsgBox(0, "", $iGet) it was get number like 52503058
      But, I want to get only student code 5250. (Different student have different code, sometime its 3 digits, Sometime 4)

    • By jmp
      I am adding labour charge to total paid amount using : 
      #include <IE.au3> #include <Array.au3> $oIE = _IEAttach ("Shop") $oTable = _IETableGetCollection ($oIE, 1) $aTableData3 = _IETableWriteToArray ($oTable) Local $sitem1 = $aTableData3[5][1] Local $sitem2 = $aTableData3[5][2] Local $lcharge = "10" ;add manualy using inputbox, becuase not generating online Local $atotPric = "Payable Total Price " Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.Innertext = $atotPric Then $iatotPric = $oTd.NextElementSibling.innertext MsgBox (0, "2", $iatotPric) EndIf Next $irCtotal = StringFormat("%.2f", $sitem1 + $sitem2 + $lcharge) $crTotp = StringReplace(_IEBodyReadHTML($oIE), $iatotPric, $irCtotal) _IEBodyWriteHTML ($oIE, $crTotp) But, It was also changing Total price, I want to change only Payable Total Price.

    • By nacerbaaziz
      hello sirs
      i've some questions about StringRegExpReplace i hope you can help me
      i tried to make a function that give me the host of the url and other give me the url with out host
      for example i've this link
      i need the first give me the
      and the other give me 
      i find that
      $s_source = "https://www.google.com/vb/index.php" Local $s_Host = StringRegExpReplace($s_Source, '.*://(.*?)/.*', '\1') Local $s_Page = StringRegExpReplace($s_source, '.*://.*?(/.*)', '\1') msgBox(64, $s_Host, $s_Page)  
      but i found some problems i need your help to correct it
      first: when i get the host if the url has www i want to remove it
      second: if the url with out host did not have other things 
      i need the result to be ""
      the first i want it
      and the second i want it to be ""
      i hope that you can help me
      thanks in advance
    • By fs1234
      I would like to change the hungarian characters in a string, but I can't figure out how to do it.
      Help, pls.
      #include <MsgBoxConstants.au3> Local $sInput = "Árvíztűrő tükörfúrógép" Local $sOutput = StringRegExpReplace($sInput, "(?-i)(á)|(Á)|(é)|(É)|(í)|(Í)|(ó)|(Ó)|(ö)|(Ö)|(ő)|(Ő)|(ú)|(Ú)|(ü)|(Ü)|(ű)|(Ű)", "(?1a)(?2A)(?3e)(?4E)(?5i)(?6I)(?7o)(?8O)(?9o)(?10O)(?11o)(?12O)(?13u)(?14U)(?15u)(?16U)(?17u)(?18U)") Display($sInput, $sOutput) Func Display($sInput, $sOutput) ; Format the output. Local $sMsg = StringFormat("Input:\t%s\n\nOutput:\t%s", $sInput, $sOutput) MsgBox($MB_SYSTEMMODAL, "Results", $sMsg) EndFunc ;==>Display  
    • By Skysnake
      I need some regex help
      I inherited some data 
      The data is massive and I need a clean, fast solution 
      source is text and complex.  
      I need to find dates such as "31-01-2018" and replace with "31-JAN-2018"
      Problem is that my regex "31-01-2018" takes for ever and replaces all.
      The ideal would be to search like this, but I am not managing
      \d{2}-(01)-\d{4} replace (01) with JAN But if I do it that way, the entire search string gets replaced by JAN.  This is not an error, but typically regex behaviour.  Any ideas?
  • Create New...