well. you just canīt apply a regex that is suppused to match multiple lines repeatedly to single lines. that wonīt ever work.

this will do the job:
#!/usr/bin/perl use warnings; use strict; my $infile = "input.txt"; my $outfile = "output.txt"; my $text; { local $/=undef; #set line separator locally to undef to read in th +e file as a whole open(FILE, $infile) or die "died opening $infile for input: $!\n"; $text = <FILE>; close(FILE); } open OUT, ">$outfile" or die "died opening $outfile for output: $!\n"; #repeatedly match searched text, while deleting found occurences #to avoid endless loop while ( $text =~ s/([A-Z]+) *- *([^-]+) *- *([^"]+)"([^"]+)".+?([0-9]+ +)$//sm ) { print OUT "CATEGORY $1\n\n", "KEYWORDS $2\n\n", "Summar text: $3\n\n", "Reference: $4\n\n", "Id: $5\n\n", } close OUT;
i suggest reading perlre

Update:

this one is better because it uses \G to avoid unneccessary backtracking
#!/usr/bin/perl use warnings; use strict; my $infile = "input.txt"; my $outfile = "output.txt"; my $text; { local $/=undef; #set line separator locally to undef to read in th +e file as a whole open(FILE, $infile) or die "died opening $infile for input: $!\n"; $text = <FILE>; close(FILE); } open OUT, ">$outfile" or die "died opening $outfile for output: $!\n"; #repeatedly match searched text, using \G #to avoid endless loop while ( $text =~ m/\G.*?([A-Z]+) *- *([^-]+) *- *([^"]+)"([^"]+)".+?([ +0-9]+)$/gsm ) { print OUT "CATEGORY $1\n\n", "KEYWORDS $2\n\n", "Summar text: $3\n\n", "Reference: $4\n\n", "Id: $5\n\n", } close OUT;

In reply to Re^3: Working with regexes by holli
in thread Working with regexes by Anonymous Monk

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.