Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: How can I capitalize a regex match?

I am using this regular expression to find patterns in a genome.

$string =~ /(?i)a+t?|(?i)t+/g

To make the output easier to read I would like to modify it so it capitalizes anything it matches that is 4 to 7 characters long. Also it should not mess up the $+[0] or $-[0] variables.

the way i do the output is to get a sub-string from the larger string file based on the '$+[0]' and '$+[0]' i don't want to print out the regex matches i am printing out huge strings of correctors and i want the matches to stand out.

if you really need to see the code I'm working on you can get it here

like image 763
Orion Avatar asked Jun 28 '11 15:06

Orion


1 Answers

With appropriate tests (for your match and for length (character groups with quantifiers like {4,7} are probably needed), without example content this is left to you) you could use an eval substituton s/(match)/uc($1)/eg which would take the matched string and make it uppercase then replace the match with the replacement.

As always read more at perldoc perlre perldoc perlreref perldoc perlretut

As a sidenote, I have always wondered if Genomes are a good candidate for Regexp::Grammars?

like image 115
Joel Berger Avatar answered Oct 28 '22 05:10

Joel Berger