Replacing with backreference in TextMate issue

前端 未结 2 1162

I am using TextMate to replace expression [my_expression] consisting in characters between open and closed brackets by {my_expression}; so I tried to r

相关标签:
2条回答
  • 2021-01-29 09:18

    You forgot to escape a character, [^]] should be [^\]].

    You also need a capture group. $1 is back-referencing the 1st Capture Group, and you had no capture groups, so use the following Regex:

    \[([^\]]*)\]
    

    This adds () around [^\]]*, so the data inside the [] is captured. For more info, see this page on Capture Groups


    However, this RegEx is shorter:

    \[(.*?)\]
    

    Also substituting with {$1}

    Live Demo on Regex101

    0 讨论(0)
  • 2021-01-29 09:22

    Use a capturing group (...):

    \[([^\]]*)\]
    

    The $1 is a backreference to the text enclosed with [...].

    Here is the regex demo and also Numbered Backreferences.

    Also, the TextMate docs:

    1. Syntax elements
      (...) group

    20.4.1 Captures
    To reference a capture, use $n where n is the capture register number. Using $0 means the entire match.

    And also:

    • If you want to use [, -, ] as a normal character in a character class, you should escape these characters by \.
    0 讨论(0)
提交回复
热议问题