in reply to Re: Problem: how to split long words
in thread Problem: how to split long words

Thank your for you code.
It works fine with a valid input i.e [tag].

If we do not check for correct [] pairs and fix it, it doesn't work. I understand it was not the task but still... We cannot just count the number of brackets in the string to check. The check has to be more complicated checking actually tags. To open a tag use '[', to close the tag use ']' not ']]' or not '[['.

I think a little check might help. Split the word followed by ']' if the last bracket is not '['.
$_ = 'sss[[]sssss]sssssss'; print $_."\n"; my $brackets = qr(\[[^\]]*\]); # text enclosed in brackets my $char = qr([^\[\]\s]); # not spaces or brackets s{ ( # group to $1 (?: $char # a char (?:$brackets*) # followed by any number of brackets ) {3} # 3 times ) (?!(?:$brackets*)(?:\s|\Z)) # ADDED: eliminates '-' on the end of wo +rds w/ a multiple of 3 chars (?= # looking forward to ensure that [^\[\]]* # we are not inside of a brackets (?: \[ | \z ) ) } {$1-}gx; print;
Thank you.