1

Searched pattern looks like text9

I search for (text)9

I want to replace with \15 so that I would get text5 but instead it's just giving me text.

Any other character works except for digits.

Tomulent
  • 531
  • 2
  • 5
  • 20

2 Answers2

2

The replacement term \15 is being interpreted as "group 15" - you must escape the "5":

Try replacing with \1\\5, or if that doesn't work (I don't have textwrangler handy) use a look behind:

Search: (?<=text)9
Replace: 5

The look behind doesn't consume input, so only the "9" is matched.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I expected that there was some method of escaping that I didn't know, unfortunately the double slash wasn't it. The look behind works, though. Thanks for helping me broaden my grepping! – Tomulent Aug 07 '15 at 20:15
2

As it turns out, the PCRE-style back-references do not work.

So, you have to use \015 to replace with the text captured with the first capturing group (\01) and 5.

Since there cannot be more than 99 capturing groups, and both the digits after \ are treated as back-reference group number, \01 is interpreted as the reference to the first group, and the rest are literal digits.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Curiously neither of those are working. I'm getting ${1}5 or \{1}5 when I try those. Could there be some obscure option I'm not ticking? Does the file type affect anything? BTW thanks for showing me regex101! – Tomulent Aug 07 '15 at 22:22
  • \015 did it, but the second string didn't – Tomulent Aug 07 '15 at 22:43
  • Excellent! I was despairing after trying \{1}5 in a similar problem and getting nowhere. I think sed uses something like that in cases like this, and it came up in some searches. I hate that I didn't think of a leading zero, but I don't think I knew that there was an explicit limit of 2 digits. The syntax coloring in BBEdit immediately showed me that it was working. – cycollins Oct 14 '20 at 22:57