Hi!
I am new to perl and I have to get a script ready for my research. I have to analyse a huge string and extract a substring between two keywords dIonly and workset with two parenthesis attached. Right now I am stuck at the foreach loop. Everything else seems to be running, the string gets slurped I have to reverse the words but not the characters in the string, because there are a lot of unwanted worksets, and I just want to go from dIonly to the first workset(( including some text and then two parenthesis at the end )), hence starting from workset(( it is like a nested regex, first I want from dIonly to workset(( including what is coming right after.
I would really appreciate help!
Thank you very much!
This is an example of my string :
originally it would have:
workset((ab;joiret;garg)) c wasdobao; erhgahufdgah; c workset((adsghlia) c aghaoeriarg;oi c aasdfgohaerg c workset(empty) c ah;sorguiaerg c aoi;hgruio;ghaer c playA c dIonly
but I am reversing it for the above reasons to:
dIonly c ....................................workset((.......)) and I need the items in the workset as well.
<code> #!/usr/bin/perl # perl -d ./perl_debugger.pl use strict; use Data::Dumper qw(Dumper); use File::Slurp; my @a_linesorig; my @a_out; my @a_str; my $line; my $reversedline; my @a_linesrev; my @reversedarray; my $reversedline; my $str; open(my $fh, "<", "data.txt") or die "cannot open < data.txt: $!"; my $line = read_file('data.txt'); @a_linesorig = split(' ', $line); @a_linesrev = reverse(@a_linesorig); $reversedline = join(' ', @a_linesrev); # joins the reversed list t +o a single string again @reversedarray = split( /solution/, $reversedline ); # should split + huge string into a list from one solution to next foreach $str (@reversedarray) { if ($str =~ /\bdIonly:\b(.*?)\bworkset\b\\(\\(/g); print (@a_out, "$str"); } close $fh or die "can't close file: $!"; open(my $fh, ">", "output.txt") or die "cannot open > output.txt: $!"; foreach $str (@a_out) { print ($fh "$str\n"); } close $fh or die "can't close file: $!";
</code>

In reply to regex - problem with the loop I believe or maybe the regex itself ? by trummelbummel

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.