I have a pattern that I'm looking to do a search and replace on, but I am finding that the S&R is only occurring about 2/3 of the time. I looked around a bit, and found this in the perlop page:
Now, the PATTERN I've been searching for is a complicated one. I was having trouble setting it up initially, and asked for help here and here. The solution I eventually wound up using was a modification of the one provided by johngg, seen here. The pertinent bits of the code I'm using are pasted below (use strict and warnings are both in use, all variables are declared, etc. This is just a snippet)Occasionally, you can't use just a /g to get all the changes to occur. + Here are two common cases: # put commas in the right places in an integer 1 while s/(.*\d)(\d\d\d)/$1,$2/g; # perl4 1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g; # perl5 # expand tabs to 8-column spacing 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
The problem is, if I search for something where $pattern[$i] = "TTT|AAA", I will get only about 2 out of every three AAAs or TTTs flanked by the <span> tags.# patterns to search with are stored in @pattern Array # patterns are strings of uc DNA (i.e. TTTAGT, etc) # but can be interrupted by lc or non-DNA characters (i.e., html tags) # searching for a needle in a haystack: my $notNeedle = q{[^ACGTYRMKSWBDHVN]*}; while ($g < $num) { $needle[$g] = $pattern[$g]; @{$parts[$g]} = split m/(\[|\]|\|)/, $needle[$g]; my @array = @{$parts[$g]}; my @results; while (@array) { my $string = shift @array; if ($string eq '[') { $string .= (join "", splice(@array, 0, 2)); } if ($string eq '|') { my $prev = pop @results; $string = (join "", $prev, $string, splice(@array, 0, 1)); } push @results, $string; } @{$parts[$g]} = @results; $haystackPatt[$g] = q{(} . join($notNeedle, @{$parts[$g]}) . q{)}; $rxHaystack[$g] = qr{$haystackPatt[$g]}; $g++; } $text_seqs[$i] =~ s{$rxHaystack[$j]}{<span class=$class[$a]>$1</span>} +gemx;
Does anyone have any ideas why this isn't replacing all of the PATTERNS? Any help, ideas, or links pointing to possible explanations would be greatly appreciated.
Thanks
Matt
In reply to s/// only replacing most of the time... by mdunnbass
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |