Loading ...
Sorry, an error occurred while loading the content.

List of Regex Matches

Expand Messages
  • Nathan Neff
    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
    Message 1 of 4 , Jun 30, 2009
    View Source
    • 0 Attachment
      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

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Tony Mechelynck
      ... The following is untested. SINGLE quotes are important. ... If I didn t goof, this should display the string after removing from it all nonalpha characters
      Message 2 of 4 , Jun 30, 2009
      View Source
      • 0 Attachment
        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
        -~----------~----~----~----~------~----~------~--~---
      • Nathan Neff
        ... Thanks Tony. I ended up modifying it a bit -- I put the + quantifier after the l, and I removed the . because it was keeping the _ and . chars in the
        Message 3 of 4 , Jun 30, 2009
        View Source
        • 0 Attachment
          > :echo substitute(string, '\.\zs\A\|\a\zs\l', '', 'g')

          Thanks Tony. I ended up modifying it a bit -- I put the + quantifier
          after the \l, and I removed the \. because it was keeping the _ and .
          chars in the
          result.

          Here's my tweaks to your suggestion:

          let string="FruitCompanyController_foo_bar_quk.groovy"
          echo substitute(string, '\a\zs\l\+\|\zs\A', '', 'g')
          >>FCCfbqg

          Thanks again!

          --Nate

          On Tue, Jun 30, 2009 at 3:56 AM, Tony
          Mechelynck<antoine.mechelynck@...> wrote:
          >
          > 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
          -~----------~----~----~----~------~----~------~--~---
        • Tony Mechelynck
          ... Hm. Adding + after l is the right thing to do, I think; it matches any number of consecutive noninitial lowercase in one fell swoop. AFAIK, with this
          Message 4 of 4 , Jun 30, 2009
          View Source
          • 0 Attachment
            On 01/07/09 00:03, Nathan Neff wrote:
            >
            >> :echo substitute(string, '\.\zs\A\|\a\zs\l', '', 'g')
            >
            > Thanks Tony. I ended up modifying it a bit -- I put the + quantifier
            > after the \l, and I removed the \. because it was keeping the _ and .
            > chars in the
            > result.
            >
            > Here's my tweaks to your suggestion:
            >
            > let string="FruitCompanyController_foo_bar_quk.groovy"
            > echo substitute(string, '\a\zs\l\+\|\zs\A', '', 'g')
            >>> FCCfbqg
            >
            > Thanks again!
            >
            > --Nate

            Hm.

            Adding \+ after \l is the right thing to do, I think; it matches any
            number of consecutive noninitial lowercase in one fell swoop.

            AFAIK, with this regexp the second \zs (after \|) isn't necessary. Then
            adding a * after the \A may (or may not - it's a guess) make it work
            faster on strings containing many clustered nonalpha.

            If your string starts with a nonalphabetic character, you'll remove it
            (because you removed the . I put to try and test for it) but maybe you
            don't care about that. If you do, you may want to study
            :help E59
            :help /zero-width
            and what they resend to.

            Also, please be aware of the fact that _bottom_-posting is preferred on
            this list.


            Best regards,
            Tony.
            --
            What sane person could live in this world and not be crazy?
            -- Ursula K. LeGuin

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          Your message has been successfully submitted and would be delivered to recipients shortly.