neversaint
neversaint

Reputation: 64064

Substituting part of words in Perl regex

Given this two text:

$mir1 = 'microRNA-9a';
$mir2 =  'microRNA10a';
$mir3 =  'microrna3a';

I want to change it to:

miR-9a
miR-10a
miR-3a

In principle I'd like to replace all the variation microRNA part with miR- Is there any single regex to do that?

I tried this but not sure how to capture the digit part.

my $mirnew = $mir =~ s/microRNA(\d+)/miR-/gi;

Upvotes: 2

Views: 77

Answers (3)

Vijay
Vijay

Reputation: 67291

s/\bmicrorna.*?([\d]+a)\b/miR-$1/ig

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213371

You need to capture words, not digits after hyphen. To backreferecence to capture group, you use $1:

s/microRNA-?(\w+)/miR-$1/gi;

Upvotes: 3

RobEarl
RobEarl

Reputation: 7912

You don't actually need to capture anything, just case insensitively replace microRNA and an optional - with miR-:

s/microRNA-?/miR-/i;

Upvotes: 6

Related Questions