On 30/06/09 09:50, Nathan Neff wrote:
>
> Hello,
>
> I'd like to be able to use Vim to get characters from a string that
> fit the following criteria
>
> 1) Is the first character in the string
> OR
> 2) Is an upper-case character
> OR
> 3) Is the first character in a "word", meaning the "b" in "foo_bar" or
> the "g" in "foo.groovy"
>
> I have a Perl script which gets close:
>
> #!/usr/bin/perl
> $string = 'fooBarBazQuk_hello.world';
> @matches = $string =~ /(^\w|[A-Z]|_\w|\W\w)/g;
> print "Matches are: " . join ":", @matches;
>
> With the Perl script, I can just weed out characters like "_" and "."
> after I get my list of matches.
>
> I've tried using Vim's matchstr and the matchlist functions, but can't
> quite get the regex
> correct.
>
> Can anyone help?
>
> Thanks
> --Nate
The following is untested. SINGLE quotes are important.
:echo substitute(string, '\.\zs\A\|\a\zs\l', '', 'g')
If I didn't goof, this should display the string after removing from it
all nonalpha characters except at the very start and all lowercase
characters immediately preceded by a letter. If the string is
'fooBarBazQuk_hello.world' as in your example, it ought to display
fBBQhw
See
:help substitute()
Remove the initial \.\zs in the pattern if you want to remove nonalpha
characters even at the start.
If you want to use this repeatedly on various strings, you can of course
define it as a function:
function CamelCaseInitials(string)
return substitute(a:string, '\.\zs\A\|\a\zs\l', '', 'g')
endfunction
Best regards,
Tony.
--
Q: Where can you buy black lace crotchless panties for sheep?
A: Fredrick's of Ithaca, New York.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---