Jump to content
Sign in to follow this  
Guy_

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) )

MAIN CODE:

Test()

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 :)

But...

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  

  • Similar Content

    • 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
      https://www.example.com/vb/result.php
      i need the first give me the
      example.com
      and the other give me 
      /vb/result.php
      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 ""
      e.g
      https://www.example.com
      the first i want it
      example.com
      and the second i want it to be ""
      i hope that you can help me
      thanks in advance
    • By fs1234
      Hi,
      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?
      Skysnake
    • By luckyluke
      $t = '... 1-347-318-9643 1-347-318-9647 1-347-318-9648 1-347-318-9650 1-347-318-9651 1-347-318-9652 1-347-318-9653 1-347-318-9655 1-347-318-&nbsp;...' $pattern = '347.*?318.*?9655' $tmp = StringRegExpReplace($t, $pattern, "|||", 1) ConsoleWrite($tmp & @CRLF) However i got this output:
      ... 1-|||  1-347-318-&nbsp;...
      Why i got only that, where is the other string, i thought the output should be this:
      ... 1-347-318-9643  1-347-318-9647  1-347-318-9648  1-347-318-9650  1-347-318-9651  1-347-318-9652  1-347-318-9653  1-|||  1-347-318-&nbsp;...
×
×
  • Create New...