in reply to regex - problem with the loop I believe or maybe the regex itself ?

I just want to go from dIonly to the first workset(( including some text and then two parenthesis at the end ))
Using your original code this works for me - note that I took out the colon (:) from dIonly.
foreach $str (@reversedarray) { if ($str =~ /\bdIonly\b.*?\bworkset\b\(\(([^)]*)/ ) { print "content of workset before dIonly: $1\n"; } }
  • Comment on Re: regex - problem with the loop I believe or maybe the regex itself ?
  • Download Code

Replies are listed 'Best First'.
Re^2: regex - problem with the loop I believe or maybe the regex itself ?
by trummelbummel (Initiate) on Feb 13, 2014 at 02:20 UTC
    thanks that is a good start. So could you please tell me how I can extend the content that it returns, right now it firstly does not return the whole content of workset, but only a subpart secondly even if I apply global, it does only do it for one part, but there are several dIonly in the string, hence I would want to extract a number of these worksets.
      If I understand correctly, and ignoring the reverse line stuff, what you want to extract is everything between the last occurrence of 'workset((..' up to 'dIonly', and that there are a number of these in each line. If that is the case I would forget about reversing and just split each line on 'dIonly', then find the substring for each segment:
      my @a_out; my $line = read_file('data.txt'); if ($line =~ /\bdIonly\b/) { # remove everything after last 'dIonly' $line =~ s/(.*)\bdIonly\b.*?$/$1/; my @segments = split(/\bdIonly\b/,$line); for my $str (@segments) { if ($str =~ /.*(\bworkset\b\(\(.*)/ ) { my $workset = $1; print "workset content: $workset\n\n"; push(@a_out,$workset); } else { print "no workset\n"; } } }