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.


In reply to Re: Regexp problems by BrowserUk
in thread Regexp problems by matth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.