Sublime Forum

Is it possible to conditionally insert arbitrary characters in a replacement?

#1

Hello!

I was wondering if it is possible to conditionally enable/disable sections of a replacement string in Sublime Text’s Find/Replace UI.

As an example, I have a find string as follows:

(?x) # Allow comments
# IF_P <STAR>
(?'if' IF_P ) \s*
[*]           \s*
(
    # NOT <STAR>
	(?'not' NOT ) \s*
	[*]           \s*
)?
# EXISTS
(?'verb' ( EXISTS ) )

What I would like to do is replace matches of that find string with

OPT_IF_NOT_$VERB

IF the capture group “NOT” exists, otherwise just

OPT_IF_${VERB}

Note that this can’t be done by substituting the “NOT” capture group, as

OPT_IF_${NOT}_${VERB}

would result in

OPT_IF__EXISTS

in cases where the “NOT” capture group doesn’t exist.
(note the extra underscore)

I’ve tried using Boost-Extended format strings, since I know that Sublime Text does (or did) use Boost’s regular expression syntax, however that doesn’t seem to work.

0 Likes

#2
0 Likes