in reply to substitute instead global matching for nested matches

First of all, I had to change that first line to
while ($fc =~ /#(\d+)\s+(([^#\s]\S*\s+\S+\s+)*)/g ) {
to get it to work the way you claimed. Otherwise that first match just grabs everything up to r5.

I'm also still unclear on what sorts of things are expected on input, e.g., why is the split call using \n when the first regexp suggests that you're expecting things to be separated by general whitespace \s+ which may or may not have newlines in it (or have multiple newlines in it)?

E.g., it's possible that

join '', map {m/(\d*)\s+(?:\S+\s+\S+\s+)*(\S+\s+\S+\s+)$/ ? "#$1\n$2" : ()} split /^#/m, $fc
is what you want, but I can't tell for sure.

Replies are listed 'Best First'.
Re^2: substitute instead global matching for nested matches
by utku (Acolyte) on Mar 20, 2012 at 13:56 UTC
    Thanks for the first regexp fix, your improvement supports more input variations in some way. The second one is also handy but regexp is long, ie. you use match pattern \S+\s+\S+\s+ twice. I had thought it is reducible to one matcher. However it does its job also. I really appreciate your help it has given me new ideas, especially with the first regexp tip.