in reply to Regexp problems
I think that the specific problem you are having lies not with your regex, but with your data.
You appear to be missing an '=' character
... sequence_source "/data/ ...
^ HERE!
Your regex would probably work if that was corrected.
That said, you might like to try this regex.
my $eq_qq = qr/ \s+ = \s+ "([^"]*?)" \s* /x; #!" my $long_regex = qr/^\s+ <gene_seq\s+ id $eq_qq status $eq_qq CDS_number $eq_qq number_of_CDSs $eq_qq sequence_source $eq_qq startpos $eq_qq endpos $eq_qq startopen $eq_qq endopen $eq_qq complement $eq_qq > /x; #!" if ($line =~ $long_regex) { print "$1\n$2\n$3\n$4\n$5\n$6\n$7\n$8\n$9\n$10"; }
Once the = char is added to your data, this captures all 10 fields
c:\test>220232 3 Sanger source DNA code 1 /data/databases/flatfiles/sequences/species/genome/embl/ch1_Sp.embl 125676 126224 1 1 F
Now this version of the regex is not as strict in verification of the format of the data as your original, but it is way easier to maintain. If you really need to verify the length and type of the captured fields, you can do this once you have captured them. That way, if the data is corrupted, you will know which field is bad which would simplify the problem of correction.
Also, you can make life easier for yourself by using the regex to capture the bits your interested in directly into named vars or an array. Using the two part regex above that if statement could become either:
my @fields = $line =~ $long_regex; if (@fields == 10) { # process $fields[0] .. $fields[9] here }
or
my ($id, $status, $CDS_number, $number_of_CDSs, $sequence_source, $startpos, $endpos, $startopen, $endopen, $complement) = $line =~ $long_regex; # Updated: Added ~ if ( $id =~ /\d{0,6}/ ) { print "ID:", $id, "appears to be valid\n"; # Use $id... } ...
There are many ways to improve this further, but it may get you started.
Update: It might also be worth pointing out that unless you are in control of the source of this data, or the sample you gave is not a complete line, this is not XML. It is XML-like, but unless there is a closing tag or you ommited the / before the final >, then non of the XML parsers are likely to help you.
Examine what is said, not who speaks.
|
|---|