Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime text snippets substitutions with 3 regex replacements

I am trying to make a snippet that will do 3 things depending on the input of one variable (router interface name). I have made the regex working correctly but its working separately.

remove xx-           >>>>>       IS-${310/\w{2}-//g}-BUSINESS
replace / with _     >>>>>       IS-${310/\//_/g}-BUSINESS
ae make it capital   >>>>>       IS-${310/(ae)/\U$1/g}-BUSINESS

So if I enter a normal router interface, like ge-1/2/21

The result should be > IS-1_2_21-BUSINESS

But if I enter an interface like ae31

it should make it capital >>>> IS-AE31-BUSINESS

like image 219
A.D usa Avatar asked Jan 29 '26 00:01

A.D usa


1 Answers

Your question doesn't include what you want your final snippet to look like, so here's a simple example of a snippet that combines all three of those operations into one, which you can adjust as needed.

<snippet>
    <content><![CDATA[
# ${310}
IS-${310/(ae)|(?:(\w{2}-))|(?:(\/))/(?1\U$1:)(?2:)(?3:_)/g}-BUSINESS
]]></content>
    <tabTrigger>test</tabTrigger>
</snippet>

The overall structure is ${variable/regex/format_string/options}. The regex uses a Boost library regular expression and the format string uses a Boost library format string (see the page on snippets in the unofficial documentation for more information).

The Boost format string supports a conditional replacement in the form of (?Ntrue:false), where for capture group N, the replacement is the text true if the match captured anything or false if it did not.

Here each of the possible alternations in the regex is captured (or not) and the replacement specifies what that matched group should expand to. Note that when doing something like this you may need to be aware of the interplay between the different possible matches.

Snippet in action

like image 119
OdatNurd Avatar answered Jan 30 '26 22:01

OdatNurd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!