WriteBackCMO
WriteBackCMO

Reputation: 619

how to make a one-digit number a capture group

I want to connect the below in perl

ab0 with NC_ab0
ab1 with NC_ab1
...

I want to use something like

Connect ab\d ab${1}.

However, the \d can't form a capture group. Any idea how I can make it a capture group and use ${1} to refer to it?

Thanks

Upvotes: 0

Views: 50

Answers (1)

Miller
Miller

Reputation: 35198

Just put parenthesis around the \d if you want to match that value to somewhere else in your regex.

Then use \1 to refer to the capture group in the LHS of the regex, as $1 is meant for use in the RHS of a substitution.

use strict;
use warnings;

while (<DATA>) {
    if (/ab(\d) with NC_ab\1/) {
        print;
    }
}

__DATA__
ab0 with NC_ab0
ab1 with NC_ab1
ab1 with NC_ab5
ab3 with NC_ab1

Outputs:

ab0 with NC_ab0
ab1 with NC_ab1

This is my current best guess by what your question meant. If this is incorrect, please reframe the question.

Upvotes: 4

Related Questions