in reply to regex: help for improvement
All the spaces that you want to insert can be added in one go, with the logic:
The (?<= ) (?= ) are Lookaround Assertions that do exactly what it says on the tin, and check around the current position, without including the checked value in the match (so the matched letters on both side aren't removed).while (my $name = <DATA>) { chomp($name); $name =~ s/ [^a-zA-Z]+ # Non letter chars | # (?<= [a-zA-Z] ) # Something that comes after a let +ter (?= [A-Z] ) # and comes before an uppercase le +tter / /xg; # \u is short for ucfirst and \L for lc $name =~ s/(\w+)/\u\L$1/g; say $name; }
Edit: you can chain s/// operations if you return the result with /r, but it's not very elegant:
say s/[^a-zA-Z]+|(?<=[a-zA-Z])(?=[A-Z])/ /gr =~ s/(\w+)/\u\L$1/gr for +<DATA>;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regex: help for improvement
by AnomalousMonk (Archbishop) on Dec 14, 2018 at 19:40 UTC | |
|
Re^2: regex: help for improvement
by frazap (Monk) on Dec 14, 2018 at 14:33 UTC | |
by Laurent_R (Canon) on Dec 14, 2018 at 18:23 UTC | |
by frazap (Monk) on Dec 20, 2018 at 14:05 UTC | |
by 1nickt (Canon) on Dec 20, 2018 at 14:31 UTC |