Reputation: 1
suppose $dna = "aaaaccccttttaaaaggggaaaacccccaaaaggggaaaacccctttttttt"
I want to cut between each aaaa & cccc in thee string and put on new line. For example, I want to get
aaaa
ccccttttaaaaggggaaaa
cccccaaaaggggaaaa
cccctttttttt
my code:
if ($DNA =~ /(.*)$match(.*)/) { # $match would be aaaacccc together
my $fragment1 = $1.$pre-match; # pre-match is aaaa
my $fragment2 = $post-match.$2; # post-match is cccc
print"$fragment1\n$fragment2\n";
I want to cut on every match.
Very Important, dna CANNOT be cut if only post-match. There must be a pre-match
Let me know what I am doing wrong. Thank you
Upvotes: 0
Views: 66
Reputation: 1027
I think the simplist way is to add line breaks with a substitution:
$dna =~ s/aaaacccc/aaaa\ncccc/g;
Upvotes: 0
Reputation: 22821
This will do what you want:
$dna = "aaaaccccttttaaaaggggaaaacccccaaaaggggaaaacccctttttttt";
$dna =~ s/(.*?)(cccc.*?)/$1\n$2/g;
print "$dna\n";
The regex will add a newline before every cccc
Outputs:
aaaa
ccccttttaaaaggggaaaa
cccccaaaaggggaaaa
cccctttttttt
Upvotes: 1
Reputation: 2935
One solution is to split the string and join it with "\n"
:
use warnings;
use strict;
use 5.01;
my $dna = "aaaaccccttttaaaaggggaaaacccccaaaaggggaaaacccctttttttt";
my ($split_left, $split_right) = ('aaaa','cccc');
say join "\n", split /(?<=$split_left)(?=$split_right)/, $dna;
Post match and pre match are predefined terms in perl and mean the whole string before and after the last match (you can access them with the /p
flag via ${^PREMATCH}
and ${^POSTMATCH}
). Also variable names in perl can't contain -
since it is an operator, use _
or camelCasing instead.
Upvotes: 0