[EMAIL PROTECTED] wrote: > > Dear Perl experts, > > I'm trying to find the right regular expressions to do some simple (?) > string processing. Can anyone tell me how to do these? #!/usr/bin/perl -lw use strict;
> 1. Given a string consisting of substrings delimited by colons, such as > :B520:L201:M:M260:8:G607:, how can I > a. remove the single-character and all-numeric substrings (M and 8 in > this example), leaving the rest alone? my $aa = ':B520:L201:M:M260:8:G607:'; my @bb = split /:/, $aa; #split on : shift @bb; # remove the andef from before the first colon my @answer1a = grep {!/^\w$|^\d+$/} @bb; > b. remove all but the single-character and all-numeric substrings and > leave the rest alone? my @answer1b = grep { /^\w$|^\d+$/} @bb; > c. remove all but single- or double-character substrings and > all-numeric substrings and leave the rest alone? > The string will never have regex metacharacters in it, just mixed alpha > and numeric, all-alpha, or all-numeric. The colons can stay. my @answer1c = grep { !/.|..|^\d+$/} @bb; > 2. Is there an easy way to count the number of substrings (the "chunks" > between colons)? my $answer2 = @bb; print "@[EMAIL PROTECTED]@bb\n$answer2\n"; > 3. This one is probably a bit difficult. I don't need to have it, but it > would save me lots of effort if I had it. Given two strings of the same > form as in no. 1, is there a regular expression which can compare the two > and return the number of substring positions which have exact matches? > I.e., given string 1 = :L000:W000:M:M260:G607: and string 2 > = :L001:W000:M:M261:M260: can match the substrings and their positions and > return the result "2" in this case? The M260 substring is present in both > but in different positions and shouldn't be counted as a match. my @array; while(<DATA>){ chomp; @bb = split /:/, $_; #split on : shift @bb; # remove the andef from before the first colon push @array, [EMAIL PROTECTED]; } my @answer; foreach my $i (0..$#array){ foreach my $j ($i+1 .. $#array){ my $nr = 0; my $k = 0; while (exists $array[$i][$k] and exists $array[$j][$k]){ $nr++ if $array[$i][$k] eq $array[$j][$k]; $k++; } print "row $i and $j have $nr equal fields"; } } __DATA__ :L000:W000:M:M260:G607: :L001:W000:M:M261:M260: /Stefan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]