Regular expression search replace in Sublime Text 2

前端 未结 6 1489
鱼传尺愫
鱼传尺愫 2020-11-27 09:10

I\'m looking to do search replace with regular expressions in Sublime Text 2. The documentation on this is rather anemic. Specifically, I want to do a replace on groups, so

相关标签:
6条回答
  • 2020-11-27 09:21

    Important: Use the ( ) parentheses in your search string

    While the previous answer is correct there is an important thing to emphasize! All the matched segments in your search string that you want to use in your replacement string must be enclosed by ( ) parentheses, otherwise these matched segments won't be accessible to defined variables such as $1, $2 or \1, \2 etc.

    For example we want to replace 'em' with 'px' but preserve the digit values:

        margin: 10em;  /* Expected: margin: 10px */
        margin: 2em;   /* Expected: margin: 2px */
    
    • Replacement string: margin: $1px or margin: \1px
    • Search string (CORRECT): margin: ([0-9]*)em // with parentheses
    • Search string (INCORRECT): margin: [0-9]*em

    CORRECT CASE EXAMPLE: Using margin: ([0-9]*)em search string (with parentheses). Enclose the desired matched segment (e.g. $1 or \1) by ( ) parentheses as following:

    • Find: margin: ([0-9]*)em (with parentheses)
    • Replace to: margin: $1px or margin: \1px
    • Result:
        margin: 10px;
        margin: 2px;
    

    INCORRECT CASE EXAMPLE: Using margin: [0-9]*em search string (without parentheses). The following regex pattern will match the desired lines but matched segments will not be available in replaced string as variables such as $1 or \1:

    • Find: margin: [0-9]*em (without parentheses)
    • Replace to: margin: $1px or margin: \1px
    • Result:
        margin: px; /* `$1` is undefined */
        margin: px; /* `$1` is undefined */
    
    0 讨论(0)
  • 2020-11-27 09:25

    Here is a visual presentation of the approved answer.

    0 讨论(0)
  • 2020-11-27 09:34

    Looking at Sublime Text Unofficial Documentation's article on Search and Replace, it looks like +(.+) is the capture group you might want... but I personally used (.*) and it worked well. To REPLACE in the way you are saying, you might like this conversation in the forums, specifically this post which says to simply use $1 to use the first captured group.

    And since pictures are better than words...

    Before:

    After:

    0 讨论(0)
  • 2020-11-27 09:39

    Note that if you use more than 9 capture groups you have to use the syntax ${10}.

    $10 or \10 or \{10} will not work.

    0 讨论(0)
  • 2020-11-27 09:42

    Usually a back-reference is either $1 or \1 (backslash one) for the first capture group (the first match of a pattern in parentheses), and indeed Sublime supports both syntaxes. So try:

    my name used to be \1
    

    or

    my name used to be $1
    

    Also note that your original capture pattern:

    my name is (\w)+
    

    is incorrect and will only capture the final letter of the name rather than the whole name. You should use the following pattern to capture all of the letters of the name:

    my name is (\w+)
    
    0 讨论(0)
  • 2020-11-27 09:43

    By the way, in the question above:

    For:

    Hello, my name is bob
    

    Find part:

    my name is (\w)+
    

    With replace part:

    my name used to be \1
    

    Would return:

    Hello, my name used to be b
    

    Change find part to:

    my name is (\w+)
    

    And replace will be what you expect:

    Hello, my name used to be bob
    

    While (\w)+ will match "bob", it is not the grouping you want for replacement.

    0 讨论(0)
提交回复
热议问题