0

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

4
  • Do you realize what (?:...) does? Commented Sep 10, 2024 at 10:19
  • Use capturing groups in the regex pattern if you need to use backreferences in the replacement patterns. See an example. Commented Sep 10, 2024 at 10:20
  • @ anubhava - No, I've never done a regex before and my search pattern is the result of much guesswork... thanks Commented Sep 10, 2024 at 10:55
  • @ Wiktor Stribizwe - Thanks your example has got me much further along and my $1 is being substituted. How can I add a condition to yours to find strings of a specific length please. So for example only find all Pnnnnn (5 chars) as opposed to Pnnn (4 Chars) Commented Sep 10, 2024 at 10:58

1 Answer 1

-1

Use the following find and replace with capturing groups:

Find:    ^(P\d+)
Replace: $1;
Sign up to request clarification or add additional context in comments.

2 Comments

@ Tim Biegeleisen - Thanks this also worked. How can I add a condition to solution to find strings of a specific length please. So for example only find all Pnnnnn (P followed by 5 chars followed by a space) as opposed to Pnnn (4 Chars)
@grayweb Use e.g. ^Pn{5} for Pnnnnn or ^Pn{4} for Pnnnn.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.