1

I want to find a way to add a leading zero "0" in front of numbers but BBEdit thinks it's substitute #10 Example:

Original string: Video 2-1: Title Goes Here

Desired result: Video 2-01: Title Goes Here

My find regex is: (-)(\d:)

My replace regex is: \10\2. The first substitute is NOT 10. I simply intend to replace first postion, then add a "0", then replace second position.

Kindly tell me how to tell BBEdit that I want to add a zero and that I don't mean 10th position.

Emma
  • 27,428
  • 11
  • 44
  • 69

4 Answers4

1

If you simply need a number preceded by a dash, then I recommend using the regex lookbehind for this one.

Try this out:

(?<=-)(\d+:)

As seen here: regex101.com

It tells the regex that the match should be preceded by a dash -, and the - itself won't be matched!

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
1

You really don't need to capture hyphen in group1 (as it is a fixed string so no benefit capturing in group1 and replacing with \1) for replacement, instead just capture hyphen with digit using -(\d+:) and while replacing just use -0\1

Regex Demo

Also, there are other better ways to make the replacement where you don't need to deal with back references at all.

Another alternate solution is to use this look around based regex,

(?<=-)(?=\d+:)

and replace it with just 0 which will just insert a zero before the digit.

Regex Demo with lookaround

Another alternate solution when lookbehind is not supported (like in Javascript prior to EcmaScript2018), you can use a positive look ahead based solution. Basically match a hyphen - which is followed by digits and colon using this regex,

-(?=\d+:)

and replace it with -0

Regex Demo with only positive look ahead

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
0

Try \1\x30\2 as the replacement. \x30 is the hex escape for the 0 character, so the replacement is \1, then 0, then \2, and cannot be interpreted as \10 then 2. I don't know if BBEdit supports hex escapes in the replacement string though.

cygri
  • 9,412
  • 1
  • 25
  • 47
-1

This expression might help you to do so, if Video 2- is a fixed input:

(Video 2-)(.+)

If you have other instances, you can add left boundary to this expression, maybe something similar to this:

([A-Za-z]+\s[0-9]+-)(.+)

Then, you can simply replace it with a leading zero after capturing group $1:

enter image description here

Graph

This graph shows how the expression would work:

enter image description here

If you wish, you can add additional boundaries to the expression.

Replacement

For replacing, you can simply use \U0030 or \x30 instead of zero, whichever your program might support, in between $1 and $2.

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
  • OP said the tool they use (BBEdit) incorrectly interprets the substitution `\10\2` as `\10` `\2` instead of `\1` `0` `\2`. This answer relies on the same substitution so will have the same problem. – cygri May 04 '19 at 18:23