Is conditional replacement possible in EmEditor?

Started by: spiros3 voices15 posts

In Notepad++ Using as text: ABCD And running: Find: (?:(A)|(B)|(C)|(D)) Replace: (?{1}E:(?{2}F:(?{3}G:(?{4}H:)))) The replacement result is: EFGH However, with Emeditor it is E)))(?{2}F:(?{3}G:(?{4}H:)))(?{2}F:(?{3}G:(?{4}H:)))(?{2}F:(?{3}G:(?{4}H:))) Is there some difference in replace syntax that is needed or is it not supported?
I am not familiar with Notepad++ replacement expressions, but you can learn EmEditor replacement expressions at: https://www.emeditor.org/en/howto_search_replacement_expression_syntax.html If you read this page, and still have any questions on a particular expression, please let me know.
Thanks, I know that page. Since EmEditor uses BoostRegex here are the references https://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html#boost_regex.format.boost_format_syntax.conditionals And from the page you quote:
If sub-expression N was matched, then true_expression is evaluated and sent to output, otherwise false_expression is evaluated and sent to output. For example, (?1foo:bar) will replace each match found with foo if the sub-expression \1 was matched, and with bar otherwise. Alternatively, you can write the expression in this form: (?{1}foo:bar)
But I still cannot figure out the syntax for the replacement as described.
For me, I'd like to use \J mode in Emeditor for conditional replacement. It's more easier to understand. For example, orginal data: 1 2 3 Search: \d Replace: \J if(\0==1){"one";}else if(\0==2){"two";}else{"three";}
Interesting! I tried Search: [A|B|C|D] Replace: \J if(\0==A){"alpha";}else if(\0==B){"beta";}else{"three";} And I got "A is undefined"
Would you please try the following: Search: [A-D] Replace: \J if("\0"=="A"){"alpha";}else if("\0"=="B"){"beta";}else{"three";}
Thanks! That works. But what if I do not want an else statement at the end? Simple omitting it does nothing. For example. Test text: прика́з, кома́нда, повеле́ние, нака́з, приказа́ние, распоряже́ние Find: [о́а́е́и́у́ю́ы́я́] Replace: \J if("\0"=="о́"){"о";}else if("\0"=="а́"){"а";}else if("\0"=="е́"){"е";}else if("\0"=="и́"){"и";}else if("\0"=="у́"){"у";}else if("\0"=="ю́"){"ю";}else if("\0"=="ы́"){"ы";}else if("\0"=="я́"){"я";} Expected result: приказ, команда, повеление, наказ, приказание, распоряжение
Your Find& Replace expression doesn't work at my side yet. If I add else{"Nothing change!";} at the end: \J if(“\0″==”о́”){“о”;}else if(“\0″==”а́”){“а”;}else if(“\0″==”е́”){“е”;}else if(“\0″==”и́”){“и”;}else if(“\0″==”у́”){“у”;}else if(“\0″==”ю́”){“ю”;}else if(“\0″==”ы́”){“ы”;}else if(“\0″==”я́”){“я”;}else{"Nothing change!";} All found character will be changed to "nothing change!". I don't know the reason. We can do it another way. Using batch replace function, you can easily achieve your goals
Thanks! Yes, I know batch replace and use it. However, I want to be able to use a conditional regex one liner instead of multiple find replace (even if used in a macro).
If I add else{“Nothing change!”;} at the end All found character will be changed to “nothing change!”. I don’t know the reason.
Apparently some sort of command should be passed to tell it to do nothing since this is JavaScript, i.e. something like null.
EmEditor uses Boost.Regex, but NOT the Boost replacement expression function. EmEditor's replacement function currently doesn't support these nested conditional expressions. If this is important, I might consider that in future versions.
It is a cool regex feature and is already implemented in Notepad++. Would be great to see it in EmEditor too!
I find another way and test. It seems works correctly. Search: ^.+$ Replace: \J "\0".replace(/о́/g,"о").replace(/а́/g,"а").replace(/е́/g,"е").replace(/и́/g,"и").replace(/у́/g,"у").replace(/ю́/g,"ю").replace(/ы́/g,"ы").replace(/я́/g,"я")
Yes, it works for me too! But there is no way to define in Find which specific characters to search instead of the whole text?
v21.9.908 includes the support for nested conditional expressions.
Oh, magic! Thank you so much for this.

15 posts in this topic