Jump to content

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


Guy_
 Share

Go to solution Solved by mikell,

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_
Link to comment
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?

Link to comment
Share on other sites

  • Solution

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
Link to comment
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
 Share

×
×
  • Create New...