32

With Regex::Replace we can use $1, $2, ... to match corresponding groups. But how can I use $1 followed by number. E.g. to replace 6 with 678?

 Regex::Replace(text, "(6)", '$178');
0

3 Answers 3

51

You need to use the alternate syntax:

Regex::Replace(text, "(6)", "${1}78");
Sign up to request clarification or add additional context in comments.

Comments

3

It seems I can use $`

Regex::Replace(text, "(6)", '$1$`78');

Comments

1

You can use backreferences to capture a named group and replace that named group with whatever you want. view this link

1 Comment

It would be nice to add an example. The documentation you point at is quite bulky.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.