3

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

A.D usa
  • 69
  • 5

1 Answers1

5

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

OdatNurd
  • 21,371
  • 3
  • 50
  • 68
  • Yes, this is exactly what I need. From this now I know how to merge multiple regex in one variable. Thank you OdatNurd. – A.D usa May 01 '18 at 20:22
  • This is awesome! Never knew snippets could use regex to do dynamic/real-time manipulation of typed/pasted text strings before. Total game changer. – Max Well Nov 03 '22 at 03:33