lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I wrote:

> function Capitalize(Str)
>   return (string.gsub(Str, "(%W*)(%w)(%w*)(%W)",
>     function(LeadSpace, FirstLetter, Rest, TrailSpace)
>       return LeadSpace .. string.upper(FirstLetter) .. Rest
>         .. TrailSpace
>     end)) -- function
> end -- Capitalize

I meant to put a star after the last %W.  As it happens,
though, the next LeadSpace capture grabs anything that the
last TrailSpace capture didn't, and any uncaptured non-word
characters at the very end of the string are simply passed
on unmolested.

I also looked at Shmuel's solution, which seems to work just
as well and is much simpler -- it doesn't even mess with
anything that isn't a word, and greediness guarantees that
it gets whole words.

(Both solutions may need to be tweaked to deal with
characters that are neither whitespace nor alphanumeric,
depending on what the desired effect is.)

-- 
Aaron