Using regexp to search

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Using regexp to search

consiglieri
Hi

I have not previously used Macvim for regexp searching (only BBedit).
I can't seem to understand how to enter the regexp.

I want to use the following regexp

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

when I try it in vim I never get any hits.

I guess I'm doing something wrong when entering it.
I would be very grateful if someone could show me how i enter it?
Is it :s/ or is it just / or what?

Thanks

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Using regexp to search

Andrew Stewart

On 24 Aug 2011, at 15:11, consiglieri wrote:
> I want to use the following regexp
>
> \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
>
> when I try it in vim I never get any hits.
>
> I guess I'm doing something wrong when entering it.
> I would be very grateful if someone could show me how i enter it?
> Is it :s/ or is it just / or what?

If you just want to search, type /your-regexp<CR> (where <CR> is carriage return / enter).

If you want to search and replace, use :s/pattern/replacement/flags.

See `:help /` and `:help substitute` for more information.

Regarding the regexp itself, Vim (unfortunately) has a concept called "magic" which governs the degree to which you need to escape special characters.

Most of the time you probably want so-called very magic regexp interpretation.  To get that, type \v at the start of your pattern.  Default, no-magic regexps in Vim require almost everything to be escaped which is rather tedious.

See `:help magic` for the details about what needs to be escaped with which degree of magic.

Yours,
Andy Stewart

-------
http://airbladesoftware.com

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Using regexp to search

Marcin Sztolcman-2
In reply to this post by consiglieri
On 24 sie 2011, at 15:11, consiglieri wrote:

> I have not previously used Macvim for regexp searching (only BBedit).
> I can't seem to understand how to enter the regexp.
>
> I want to use the following regexp
>
> \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Vim has a little other regexp syntax (by default) ;)

Some of metacharacters there you need additional escape (+ -> \+, [ -> \[ etc), and replace boundaries with:
\< and \>

More:
:he regexp

--
Marcin Sztolcman :: http://urzenia.net/ :: http://sztolcman.eu/

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Using regexp to search

Steven Michalske
In reply to this post by consiglieri
http://www.vim.org/scripts/script.php?script_id=3282

might help.

I am sure there are other vim scripts to extend regex, to the flavor of regex you want.

Steve

On Aug 24, 2011, at 6:11 AM, consiglieri wrote:

> Hi
>
> I have not previously used Macvim for regexp searching (only BBedit).
> I can't seem to understand how to enter the regexp.
>
> I want to use the following regexp
>
> \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
>
> when I try it in vim I never get any hits.
>
> I guess I'm doing something wrong when entering it.
> I would be very grateful if someone could show me how i enter it?
> Is it :s/ or is it just / or what?
>
> Thanks
>
> --
> You received this message from the "vim_mac" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Using regexp to search

Charles Campbell
In reply to this post by consiglieri
consiglieri wrote:

> Hi
>
> I have not previously used Macvim for regexp searching (only BBedit).
> I can't seem to understand how to enter the regexp.
>
> I want to use the following regexp
>
> \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
>
> when I try it in vim I never get any hits.
>
> I guess I'm doing something wrong when entering it.
> I would be very grateful if someone could show me how i enter it?
> Is it :s/ or is it just / or what?
>    
OK, let's see what your regexp is matching using the help.

\b :   :he /\b shows that this matches a backspace character (is that
what you want?)

[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]  : quite a range you have here with a
lot of repeated character ranges.  For example, the +-] matches a range
of characters with integer values of 43 through and including 93.  See
:help [ and read about - near line 996 (with vim7.3).

{2,4} matches, literally the characters { 2 , 4 } .  If you happened to
want 2-4 matches of the preceding atom, see  :help /\{

\b : again, you're asking for a match to the backspace character (ascii
#8).  Did you happen, perhaps, to want to match to a whitespace?  See
:he /\s .

I suggest reading, or at least glancing through,   :help regexp ; Vim's
regular expressions are not the same as BBedit's, evidently.

Regards,
Chip Campbell

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Using regexp to search

rilahust
This post has NOT been accepted by the mailing list yet.
In reply to this post by consiglieri
it looks like a perl style regexp, maybe you should use vim style instead.
Reply | Threaded
Open this post in threaded view
|

Re: Using regexp to search

Carl Jacobsen-2
In reply to this post by consiglieri
On Wed, 24 Aug 2011, consiglieri wrote:

> I want to use the following regexp
>
> \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Others have already pointed you towards Vim's help (go read ':h regexp',
it's definitely worth the effort); for the specifics of your regexp, in
Vim, I'm guessing you want:

  \<[A-Z0-9._%+\-]\+@[A-Z0-9.\-]\+\.[A-Z]\{2,4}\>

Noting:
     - \b (for word boundaries, ala Perl), becomes \< and \>
     - must escape a literal '-' inside character classes, otherwise
       it's interpreted to be specifying part of a range
     - must escape '{' for Vim to recognize a {min,max} quantifier

> I would be very grateful if someone could show me how i enter it?
> Is it :s/ or is it just / or what?

Vim separates "search" out from "search & replace". To search for the
next occurrence of "foo", in normal mode, you'd type (ending with Enter):
  /foo [to search forward]
  or
  ?foo [to search back]

and then you can visit subsequent occurrences of the same pattern using
just "n" (to continue in the same direction) or "N" to head the opposite
way.

To replace the FIRST occurrence of "foo" with "bar" in the CURRENT line:
  :s/foo/bar/

To replace ALL occurrences of "foo" with "bar" in the CURRENT line:
  :s/foo/bar/g

To replace ALL occurrences of "foo" with "bar" in the ALL lines:
  :%s/foo/bar/g

Vim's search (and replace) commands offer huge amounts of power, in
trade for memorizing a bit of syntax.

Oh, and in MacVim, if you prefer your search options all GUI, try Cmd-F.
Me, I'm too old-school for that :)

Cheers,
Carl Jacobsen

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Using regexp to search

David Henderson

On 29 Aug 2011, at 15:50, Carl Jacobsen wrote:

>    - must escape a literal '-' inside character classes, otherwise
>      it's interpreted to be specifying part of a range

Strictly speaking not true. It is considered best practice in regexs that if the ‘-' is to be included in the expression class that it be the first character. Thusly:

\<[-A-Z0-9._%+]\+@[-A-Z0-9.\]\+\.[A-Z]\{2,4}\>

David P Henderson
--
"The proper office of a friend is to side with you when you are in the wrong. Nearly anybody will side with you when you are in the right."
    -- Mark Twain

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php