Sublime Forum

In ST regex can you use a conditional replace

#1

I’ve searched, and it’s possible I missed it, but I’m trying to find out if, using ST’s regex, you can conditionally replace.

Example:
// find either first last OR first middle last because both exist in list of names

(?<first>.+)\ (?<last>.+)|(?<first>.+)\ (?<middle>.)\ (?<last>.+)

//replace with last, first (AND middle if it was found)
//don’t know what the syntax would be so using {<variable>? true : false}
//added spaces around expression to make for easier reading

$+{last}\t$+{first}( $+{middle} ? \ $+{middle} : "" )

Thank you

0 Likes

#2

unfortunately ST doesn’t support this directly, see

but it can still easily be done via plugin API or just change your regex and replacement patterns so that the condition isn’t necessary…

(?<first>.+)(?<middle>\ .+)?\ (?<last>.+)
$+{last}, $+{first}$+{middle}
1 Like