This is a translation of the original English documentation page. Help us make it better.

17 エスケープの例

概要

このページでは、さまざまなコンテキストで正規表現を使用する際に正しいエスケープを使用する例を示します。

トリガー式コンストラクターを使用すると、正規表現に正しいエスケープが自動的に追加されます。

Examples

User macro with context

Regular expression: \.+\"[a-z]+
User macro with context: {$MACRO:regex:"\.+\\"[a-z]+"}

Notice:

  • backslashes are not escaped;
  • quotation marks are escaped.

LLD macro function

Regular expression: \.+\"[a-z]+
LLD macro: {{#MACRO}.iregsub("\.+\\"[a-z]+", \1)}

Notice:

  • backslashes are not escaped;
  • quotation marks are escaped.

LLD macro function inside user macro context

Regular expression: \.+\"[a-z]+
LLD macro: {{#MACRO}.iregsub("\.+\\"[a-z]+", \1)}
User macro with context: {$MACRO:"{{#MACRO}.iregsub(\".+\\\"[a-z]+\", \1)}"}

Notice:

  • backslash escaping for LLD does not change;
  • upon inserting the LLD macro into user macro context, we need to put it into string:
  1. Quotation marks are added around the macro expression;
  2. Quotation marks get escaped; in total, 3 new backslashes are introduced.

String parameter of non-history function

String content: \.+\"[a-z]+
Expression: concat("abc", "\\.\\\"[a-z]+")

Notice:

  • String parameters require escaping both for backslashes and quotation marks.

String parameter of history function

String content: \.+\"[a-z]+
Expression: find(__ITEM_KEY__,,"regexp","\.+\\"[a-z]+")

Notice:

  • backslashes are not escaped;
  • quotation marks are escaped.

LLD macro function inside string parameter of non-history function

Regular expression: \.+\"[a-z]+
LLD macro: {{#MACRO}.iregsub("\.+\\"[a-z]+", \1)}
Expression: concat("abc, "{{#MACRO}.iregsub(\"\\.+\\\\\"[a-z]+\", \\1)}")

Notice:

  • String parameters require escaping both for backslashes and quotation marks;
  • Another layer of escaping is added, because the macro will be resolved only after string is unqouted;

LLD macro function inside string parameter of history function

Regular expression: \.+\"[a-z]+
LLD macro: {{#MACRO}.iregsub("\.+\\"[a-z]+", \1)}
Expression: find(__ITEM_KEY__,,"eq","{{#MACRO}.iregsub(\"\.+\\\"[a-z]+\", \1)}")

Notice:

  • backslashes are not escaped;
  • quotation marks are escaped.

User macro with context inside string parameter of non-history function

Regular expression: \.+\"[a-z]+
User macro with context: {$MACRO:regex:"\.+\\"[a-z]+"}
Expression: concat("abc, "{$MACRO:regex:\"\\.+\\\\\"[a-z]+\"}")

Notice:

  • Same as in the previous example an additional layer of escaping is needed;
  • Backslashes and quotation marks are escaped only for the top-level escaping (by virtue of it being a string parameter).

User macro with context inside string parameter of history function

Regular expression: \.+\"[a-z]+
User macro with context: {$MACRO:regex:"\.+\\"[a-z]+"}
Expression: find(__ITEM_KEY__,,"eq","{$MACRO:regex:\"\.+\\\"[a-z]+\"}")

Notice:

  • backslashes are not escaped;
  • quotation marks are escaped.

LLD macro function inside user macro context inside non-history function

Regular expression: \.+\"[a-z]+
LLD macro: {{#MACRO}.iregsub("\.+\\"[a-z]+", \1)}
User macro with context: {$MACRO:"{{#MACRO}.iregsub(\".+\\\"[a-z]+\", \1)}"}
Expression: concat("abc, "{$MACRO:\"{{#MACRO}.iregsub(\\\".+\\\\\\\"[a-z]+\\\", \\1)}\"}")

Notice the three layers of escaping:

  1. For LLD macro function, without escaping of backslashes;
  2. For User macro with context, without escaping of backslashes;
  3. For the string parameter of a function, with escaping of backslashes.

LLD macro function inside user macro context inside history function

Regular expression: \.+\"[a-z]+
LLD macro: {{#MACRO}.iregsub("\.+\\"[a-z]+", \1)}
User macro with context: {$MACRO:"{{#MACRO}.iregsub(\".+\\\"[a-z]+\", \1)}"}
Expression: find(__ITEM_KEY__,,"eq","{$MACRO:\"{{#MACRO}.iregsub(\\".+\\\\"[a-z]+\\", \1)}\"}")

Notice:

  • backslashes are not escaped;
  • quotation marks are escaped.

User macro with context just inside string

Regular expression: \.+\"[a-z]+
User macro with context: {$MACRO:regex:"\.+\\"[a-z]+"}
Inside string of some expression, for example: func(arg1, arg2, arg3)="{$MACRO:regex:\"\\.+\\\\\"[a-z]+\"}"

Notice:

  • Strings also require backslash escaping;
  • Strings also require quotation mark escaping;
  • Again a case with 2 levels of escaping:
    1. Escaping for user macro context without backslash escaping;
    2. Escaping for it being a string with backslash escaping.