Print Page | Close Window

Replaceing a substring with some other substring

Printed From: MetaCase
Category:
Forum Name: MetaEdit+
Forum Description: All topics relating to MetaEdit+ or DSM
URL: http://www.metacase.com/forums/forum_posts.asp?TID=883
Printed Date: 19.Apr.2024 at 13:48
Software Version: Web Wiz Forums 12.05 - http://www.webwizforums.com


Topic: Replaceing a substring with some other substring
Posted By: mert
Subject: Replaceing a substring with some other substring
Date Posted: 03.Apr.2018 at 10:44
Dear All,

In my DSL development, I want to replace the "result" substring in a user-entered statement with the "result_"+$variable. Note here that $variable holds another user-entered data. 

I have tried the translator operator as below but it did not work. 

   to 
'%resultTranslator' newline '$result $result_$variable'
   endto 

and used the translator as follows:
:userStatement%resultTranslator;



Replies:
Posted By: stevek
Date Posted: 04.Apr.2018 at 09:08
If $variable is a MERL variable, then you'll need to use it outside string quotes - otherwise you're just asking to translate that part every time to the literal string '$variable'. For simple cases, where $variable contains just alphanumeric characters, you thus have:
to '%resultTranslator' newline '$result $result_' $variable endto
When that is evaluated with $variable='value', the translator is thus defined to translate 'result' into 'result_value'. 

If $variable can contain characters that are special to MERL translators (newline space \ / $ % - *) - then you should escape these when using it in the translator. A translator to translate escapable characters is of course pretty ugly, as the escapes need to be escaped, but here it is:
to '%escape
\' newline ' $\\\' newline '
\  $\\\ 
\\ $\\\\
\/ $\\\/
\$ $\\\$
\% $\\\%
\- $\\\-
\* $\\\*'
endto

to '%resultTranslator' newline '$result $result_' $variable%escape endto



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.05 - http://www.webwizforums.com
Copyright ©2001-2022 Web Wiz Ltd. - https://www.webwiz.net