Jump to content
Sign in to follow this  

StringRegExpReplace stuff like ' 1) a) ' to ' (1) (a) ' + 'non-capturing' relevance?

Recommended Posts

For my application, I have one string that may contain as item pointers a "space + single character/number + )"

For example:

Some text. 1) blah 2) blah a) yes b) no.

I'd like this to become:

Some text. (1) blah (2) blah (a) yes (b) no.

It seems I understand the use of the back reference $1, but I may not be grasping the functionality of non-capturing groups...

I hoped I could use the latter functionality so in the MAIN CODE I would not get the space between '(' and $1

The first code line below now giving me:

Some text. ( 1) blah ( 2) blah ( a) yes ( b) no.
Local $sOutput = StringRegExpReplace($sInput, '((?:\s).\))', ' ($1' )

As another trial, this doesn't seem to work either:

Local $sOutput = StringRegExpReplace($sInput, '(\s.\))', ' (' & StringRight('$1', 2) )



Func Test()
    Local $sInput = 'Some text. 1) blah 2) blah a) yes b) no.'
; look for 'space + any char + )' and put '(' in front
    Local $sOutput = StringRegExpReplace($sInput, '( (?:\s).\) )' , ' ($1' )
    Display($sInput, $sOutput)
EndFunc   ;==>Test

Func Display($sInput, $sOutput)
    ; Format the output.
    Local $sMsg = StringFormat("Input:\t%s\n\nOutput:\t%s", $sInput, $sOutput)
    MsgBox(0, "Results", $sMsg)
EndFunc   ;==>Display

Thank you for any pointers :)

Edited by Guy_

Share this post

Link to post
Share on other sites

You only need to find the places just followed by "a char and ) " and put here a "("  

Local $sOutput = StringRegExpReplace($sInput, '(?=\w\))' , "(" )

As this matches a position this also works in such cases

Local $sInput = '0) Some text. 1) blah 2) blah a) yes b) no.'


Edited by mikell

Share this post

Link to post
Share on other sites

Clean! Thx :)


I will also often have '(text)' or '(text).' in my string, and your code makes it '(tex(t)'

Since I'll probably almost always have a space in front and after the item text, my 'keep it simple ideal' is currently to search for

'space + any char + ) + space' and then put the '(' in.

If I just start by adding that space in your code, I already run into the old problem again of an output space in the wrong place...

Local $sOutput = StringRegExpReplace($sInput, '(?=\s\w\))' , "(" )


Therefore I was wondering if the solution could be related to the 'non-capturing groups' functionality,

allowing to search for the spaces, but not capturing / outputting them?

Share this post

Link to post
Share on other sites

Better refine the requirements for the searched position

Local $sOutput = StringRegExpReplace($sInput, '(?<=\s|^)(?=\w\))' , "(" )

Find the places just followed by "a char and ) " and preceded by "a space or start of string" (if needed)

Because if you do something like this

Local $sOutput = StringRegExpReplace($sInput, '(?:\s)(\w\))' , "($1" )

the non-capturing group is not captured but is still replaced, so the space is lost

So you'll have to grab all into backreferences

Local $sOutput = StringRegExpReplace($sInput, '(\s)(\w\))' , "$1($2" )


Edited by mikell

Share this post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ni3dprint
      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!
    • 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  
  • Create New...