regexmacosvariablessubstitution

Simple Find and Replace Text in Sublime Text Using REGEX


Sublime Text - Build 4180 on MacOS

I have built a regex to find strings of nn chars length which start with a P at the beginning of a line. I want to take the string found and add a ";" to it... so, for example, find:

P17737 Text is here
P18932 A different text is here 
P723 Some more text
P22809 Potentially another just here

And replace with

P17737; Text is here
P18932; A different text is here 
P723 Some more text
P22809; Potentially another just here

My regex looks like

^(?:P)(?:.{0,5})

My replace looks like

$1;

However, my $1 is not being substituted with the text found consequently replacing the first occurance looks like this

; Text is here             
P18932 A different text is here
P723 Some more text                                                                      
P22809 Potentially another just here 

I was expecting my $1 to be substituted with the string found but it is not? Why is that please? I have tried various combinations of curly braces, square brackets and parentheses around my $1 variable. I attach a screenshot of my attempt. Thanks.

Regex Screenshot


Solution

  • Use the following find and replace with capturing groups:

    Find:    ^(P\d+)
    Replace: $1;