in reply to regex - problem with the loop I believe or maybe the regex itself ?
G'day trummelbummel,
Welcome to the monastery.
It's much easier for us to help if you provide a short, representative example of your initial data and a clear indication of your expected output. A written description of the data is rarely, if ever, useful. The data should be shown in <code>...</code> tags. I realise this is your first post: please just keep this in mind for future reference.
I'm wondering if your question falls into the "XY Problem" category: you've focussed more on a specific solution rather than on the actual problem. Your problem seems to be that you have a text file and you need to extract some data from it. Your current solution involves manipulating the data with split(), reverse() and join(): you've said this is necessary — I'm not convinced that it is.
If you'd care to consider another solution, take a look at the following (then the Notes at the end).
#!/usr/bin/env perl -l use strict; use warnings; my $sep = '-' x 60 . "\n"; my ($start, $end) = ('workset((', 'dIonly'); my $all_to_end_incl; { local $/ = $end; $all_to_end_incl = <DATA>; } print $sep, "Up to and including first '$end':\n", $all_to_end_incl; my $start_end_incl = substr $all_to_end_incl, index $all_to_end_incl, +$start; print $sep, "From '$start' to '$end' inclusive:\n", $start_end_incl; my $start_end_excl = substr $start_end_incl, length($start), -length($ +end); print $sep, "From '$start' to '$end' exclusive:\n", $start_end_excl; my $paren_group_re; $paren_group_re = qr{ \( (?: (?> [^()]+ ) | (??{ $paren_group_re }) )* \) }x; my $workset_re = qr{ ( workset\(\( (?: [^(]+ (??{ $paren_group_re }) [, ]* )* \)\) ) }x; $start_end_excl =~ $workset_re; print $sep, "Wanted extract:\n", $1; __DATA__ ... other data before 'workset' found ... workset(( RiskCA(cA, 3) RiskCB(cB, 2)) c workset((RiskCA(cA, 3), RiskCB(cB, 2), totPaycA(cA, 7), totPaycB(c +B, 6))) *********** trial #682 ceq pAAr(rA, cA, P1) c pAAc(rA, cA, P2) c ineqAA(rA, cA, P3) = (pAAc(r +A, cA, ... rl dec c cognum(X2) c watch(X1) c worklist(L) c workset((S, maxtotIneq +C(cB, X))) => watch(X1 + 1) c cognum(X2 + 1) c worklist(nil) c workset(e +mpty) c playA c dIonly [label avoidMaxI] . X2 --> 3 X1 --> 3
That code has output at each stage: partly for demo purposes and partly because I'm not entirely sure which parts you want. Only the last part, which I'm certain you wanted, is displayed; the full output is in the spoiler.
Wanted extract: workset((RiskCA(cA, 3), RiskCB(cB, 2), totPaycA(cA, 7), totPaycB(cB, 6 +)))
------------------------------------------------------------ Up to and including first 'dIonly': ... other data before 'workset' found ... workset(( RiskCA(cA, 3) RiskCB(cB, 2)) c workset((RiskCA(cA, 3), RiskCB(cB, 2), totPaycA(cA, 7), totPaycB(c +B, 6))) *********** trial #682 ceq pAAr(rA, cA, P1) c pAAc(rA, cA, P2) c ineqAA(rA, cA, P3) = (pAAc(r +A, cA, ... rl dec c cognum(X2) c watch(X1) c worklist(L) c workset((S, maxtotIneq +C(cB, X))) => watch(X1 + 1) c cognum(X2 + 1) c worklist(nil) c workset(e +mpty) c playA c dIonly ------------------------------------------------------------ From 'workset((' to 'dIonly' inclusive: workset(( RiskCA(cA, 3) RiskCB(cB, 2)) c workset((RiskCA(cA, 3), RiskCB(cB, 2), totPaycA(cA, 7), totPaycB(c +B, 6))) *********** trial #682 ceq pAAr(rA, cA, P1) c pAAc(rA, cA, P2) c ineqAA(rA, cA, P3) = (pAAc(r +A, cA, ... rl dec c cognum(X2) c watch(X1) c worklist(L) c workset((S, maxtotIneq +C(cB, X))) => watch(X1 + 1) c cognum(X2 + 1) c worklist(nil) c workset(e +mpty) c playA c dIonly ------------------------------------------------------------ From 'workset((' to 'dIonly' exclusive: RiskCA(cA, 3) RiskCB(cB, 2)) c workset((RiskCA(cA, 3), RiskCB(cB, 2), totPaycA(cA, 7), totPaycB(c +B, 6))) *********** trial #682 ceq pAAr(rA, cA, P1) c pAAc(rA, cA, P2) c ineqAA(rA, cA, P3) = (pAAc(r +A, cA, ... rl dec c cognum(X2) c watch(X1) c worklist(L) c workset((S, maxtotIneq +C(cB, X))) => watch(X1 + 1) c cognum(X2 + 1) c worklist(nil) c workset(e +mpty) c playA c ------------------------------------------------------------ Wanted extract: workset((RiskCA(cA, 3), RiskCB(cB, 2), totPaycA(cA, 7), totPaycB(cB, 6 +)))
Notes:
-- Ken
|
|---|