in reply to regex: help for improvement

Why is della Vople D. translated to Della Volpe D, but deRenstrom P. A. Bruckman becomes de Renstrom P A Bruckman? In other words, you don't capitalize the first word if it comes from a snakeCase word.

I tried to solve this with only regexes:

#!/usr/bin/perl use warnings; use strict; use Test::More; while ( my $t = <DATA> ) { chomp $t; is new_translate($t), translate($t); } done_testing(); sub translate { # the OP's code here } sub new_translate { my ($str) = @_; $str =~ tr/-/ /; $str =~ tr/a-zA-Z/ /cs; $str =~ s/(?<=\p{isLower})(?=\p{isUpper})/ /g; $str =~ s/(?:(?<=\s)|(?<=^))(\p{isLower})/\u$1/g; $str =~ s/\s+$//r }

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: regex: help for improvement
by frazap (Monk) on Dec 14, 2018 at 14:11 UTC
    I had not seen the de Renstroem ... So the function is even not working as I was expecting.

    To treat names with accented letter (say utf8) using your function, I tried this

    my ($str) = @_; $str =~ tr/-/ /; #$str =~ tr/a-zA-Z/ /cs; my $new; while ( $str =~ m/\G([\p{isUpper}|\p{isLower}|\s]+)/g ) { $new.=$1; } $str = $new; $str =~ s/(?<=\p{isLower})(?=\p{isUpper})/ /g; $str =~ s/(?:(?<=\s)|(?<=^))(\p{isLower})/\u$1/g; $str =~ s/\s+$//r

    but it's not working since any character that is not a letter or a space break the loop and the rest is lost. How can I adapt tr/a-zA-Z/ /cs for unicode character ?

    Thanks

    F.
      Do you want to replace any sequence of non-letters by a space?
      $str =~ s/\P{isLetter}+/ /g;

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        Yes, I think that the way to go !

        Thanks

        F.
      I had not seen the de Renstroem ... So the function is even not working as I was expecting.

      frazap:   WRT the use of Test::More for posing questions (to PerlMonks or to yourself during development!), see also neilwatson's article How to ask better questions using Test::More and sample data. Of course, the essential question you're asking yourself during development is "Does this code work the way I think it works?"


      Give a man a fish:  <%-{-{-{-<

      I had not seen the de Renstroem ... So the function is even not working as I was expecting.
      That's where using a testing module such as Test::More, as suggested earlier by hippo, is really useful and handy.