## Re: Matching JUST the nth occurence of a text in a line

Expand Messages
• A.J.Mechelynck wrote in ... /^. {-}home. {-} zshome/e Was the solution I was looking for (BTW the /e indicates where the match
Message 1 of 20 , Dec 1, 2006
• 0 Attachment
"A.J.Mechelynck" <antoine.mechelynck@...> wrote in
news:456FE0F9.2080501@...:

> Bill McCarthy wrote:
>> On Thu 30-Nov-06 11:49pm -0600, Peter Hodge wrote:
>>> --- Bill McCarthy wrote:
>>>> On Thu 30-Nov-06 10:24pm -0600, Peter Hodge wrote:
>>>>> --- Bill McCarthy wrote:
>>>>>> On Thu 30-Nov-06 9:20pm -0600, Peter Hodge wrote:
>>
>>>>>>> Try:
>>>>>>>
>>>>>>> /^.\{-}home.\{-}\zshome
>>>>>>>

/^.\{-}home.\{-}\zshome/e

Was the solution I was looking for (BTW the /e indicates where the match
ends)

Thanks
--
http://successtheory.com/tips/ Vim, Zsh, MySQL Tips
• ... To generalize to the n-th occurrence: (put the qty of skipped matches in N) /^. {-} %(home. {-1,} ) {N} zshome So, for the 2nd home: let N=1. Regards,
Message 2 of 20 , Dec 1, 2006
• 0 Attachment
Peter Hodge wrote:

>Try:
>
> /^.\{-}home.\{-}\zshome
>
>
>
> \{-} makes the '.' match as little as possible
> \zs makes the search match begin at this point in the pattern
>
>
>
To generalize to the n-th occurrence: (put the qty of skipped matches in N)

/^.\{-}\%(home.\{-1,}\)\{N}\zshome

So, for the 2nd home: let N=1.

Regards,
Chip Campbell
• All, I am puzzled by a slightly more complicated version: how to match a % character following the 2nd occurrence of home ? --Matt
Message 3 of 20 , Dec 1, 2006
• 0 Attachment
All,

I am puzzled by a slightly more complicated version:
how to match a '%' character following the 2nd occurrence of "home"?

--Matt

On Fri, Dec 01, 2006 at 09:09:17AM -0500, Charles E Campbell Jr wrote:
> Peter Hodge wrote:
>
> >Try:
> >
> > /^.\{-}home.\{-}\zshome
> >
> >
> >
> > \{-} makes the '.' match as little as possible
> > \zs makes the search match begin at this point in the pattern
> >
> >
> >
> To generalize to the n-th occurrence: (put the qty of skipped matches in N)
>
> /^.\{-}\%(home.\{-1,}\)\{N}\zshome
>
> So, for the 2nd home: let N=1.
>
> Regards,
> Chip Campbell
>
• ... /^. {-} %(home. {-1,} ) {N}home. {-} zs% where N is 1 for the 2nd occurrence (N is 2 for the third occurrence, etc). This pattern matches up to and
Message 4 of 20 , Dec 1, 2006
• 0 Attachment
mzyzik@... wrote:

>I am puzzled by a slightly more complicated version:
> how to match a '%' character following the 2nd occurrence of "home"?
>
>
/^.\{-}\%(home.\{-1,}\)\{N}home.\{-}\zs%

where N is 1 for the "2nd occurrence" (N is 2 for the third occurrence,
etc).

This pattern matches up to and including the 2nd home, extends past a
minimal amount of anything up to but not including a %, whereupon the
pattern matching starts.

Regards,
Chip Campbell
• ... This pattern doesn t work. Try it on this line: home home home home home home% home It should match nothing on that line, because the % doesn t follow
Message 5 of 20 , Dec 1, 2006
• 0 Attachment
On Fri, Dec 01, 2006 at 10:34:14AM -0500, Charles E Campbell Jr wrote:
> mzyzik@... wrote:
>
> >I am puzzled by a slightly more complicated version:
> > how to match a '%' character following the 2nd occurrence of "home"?
> >
> >
> /^.\{-}\%(home.\{-1,}\)\{N}home.\{-}\zs%

This pattern doesn't work. Try it on this line:
"home home home home home home% home"

It should match nothing on that line, because the '%' doesn't follow the
2nd occurrence as desired.

--Matt

>
> where N is 1 for the "2nd occurrence" (N is 2 for the third occurrence,
> etc).
>
> This pattern matches up to and including the 2nd home, extends past a
> minimal amount of anything up to but not including a %, whereupon the
> pattern matching starts.
>
> Regards,
> Chip Campbell
>
• ... But it *does* work! You didn t specify avoiding the % if matches to the home pattern intervened. Try ... and the following two lines: home % home home
Message 6 of 20 , Dec 1, 2006
• 0 Attachment
mzyzik@... wrote:

>On Fri, Dec 01, 2006 at 10:34:14AM -0500, Charles E Campbell Jr wrote:
>
>
>>mzyzik@... wrote:
>>
>>
>>
>>>I am puzzled by a slightly more complicated version:
>>> how to match a '%' character following the 2nd occurrence of "home"?
>>>
>>>
>>>
>>>
>>/^.\{-}\%(home.\{-1,}\)\{N}home.\{-}\zs%
>>
>>
>
>This pattern doesn't work. Try it on this line:
>"home home home home home home% home"
>
>It should match nothing on that line, because the '%' doesn't follow the
>2nd occurrence as desired.
>
>
But it *does* work! You didn't specify avoiding the % if matches to the
home pattern intervened.
Try
:set hls
and the following two lines:

home % home home home home home% home
home home %home home home% home

and you'll only see %s match after the 2nd home, exactly as specified.
Now, if you change
the requirement, a different regex is needed.

Regards,
Chip Campbell
• ... I can rephrase: how to match a % character following the 2nd occurrence of home ? to how to match a % character directly following the 2nd
Message 7 of 20 , Dec 1, 2006
• 0 Attachment
On Fri, Dec 01, 2006 at 04:41:37PM -0500, Charles E Campbell Jr wrote:
> >>>I am puzzled by a slightly more complicated version:
> >>> how to match a '%' character following the 2nd occurrence of "home"?
> >>/^.\{-}\%(home.\{-1,}\)\{N}home.\{-}\zs%
> >This pattern doesn't work. Try it on this line:
> >"home home home home home home% home"
> But it *does* work! You didn't specify avoiding the % if matches to the
> home pattern intervened.

I can rephrase:
"how to match a '%' character following the 2nd occurrence of "home"?"
to
"how to match a '%' character directly following the 2nd occurrence of "home"?"

I am pretty sure everyone else knew what I meant.

--Matt

P.S. Does adding "directly" change the meaning for you? And if so, do
you know the regular expression I want?

> Try
> :set hls
> and the following two lines:
>
> home % home home home home home% home
> home home %home home home% home
>
> and you'll only see %s match after the 2nd home, exactly as specified.
> Now, if you change
> the requirement, a different regex is needed.
>
> Regards,
> Chip Campbell
>
• ... I don t have tag non-greedy in the help. I have vim 7.9.164 from svn. Where do I get more updated helpfiles ? Yakov
Message 8 of 20 , Dec 1, 2006
• 0 Attachment
On 12/1/06, Bill McCarthy <WJMc@...> wrote:
> One might think so, but also note:
>
> :help non-greedy

I don't have tag 'non-greedy' in the help. I have vim 7.9.164
from svn. Where do I get more updated helpfiles ?

Yakov
• ... 7.0.164, sorry, not 7.9.164. Still, where do I get more updated helpfiles like yours ? Yakov
Message 9 of 20 , Dec 1, 2006
• 0 Attachment
On 12/2/06, Yakov Lerner <iler.ml@...> wrote:
> On 12/1/06, Bill McCarthy <WJMc@...> wrote:
> > One might think so, but also note:
> >
> > :help non-greedy
>
> I don't have tag 'non-greedy' in the help. I have vim 7.9.164
7.0.164, sorry, not 7.9.164. Still, where do I get more updated
helpfiles like yours ?
Yakov
• ... By ftp: ftp://ftp.vim.org/pub/vim/runtime/ and all its contents except its dos/ subdirectory. By rsync: the following assumes that you compile vim
Message 10 of 20 , Dec 1, 2006
• 0 Attachment
Yakov Lerner wrote:
> On 12/1/06, Bill McCarthy <WJMc@...> wrote:
>> One might think so, but also note:
>>
>> :help non-greedy
>
> I don't have tag 'non-greedy' in the help. I have vim 7.9.164
> from svn. Where do I get more updated helpfiles ?
>
> Yakov
>

By ftp:

ftp://ftp.vim.org/pub/vim/runtime/

and all its contents except its dos/ subdirectory.

By rsync: the following assumes that you compile vim yourself, and that the
current directory is the top of your building tree: src/.. if you will. The
two additional lines copy (on Unix) the updated files to where you'll be using
them (such as /usr/local/share/vim/vim70/).

rsync -avzcP --delete --exclude="/dos/" ftp.nluug.nl::Vim/runtime/ ./runtime/
cd src
make installruntime

outdated files which don't exist anymore in the latest version of the files,
and it doesn't touch the files which are already up-to-date.

Best regards,
Tony.
• ... I see Tony already provided the an approach for unix. To update your directories on Windows (using 4nt): copy /[!*~]/u/s
Message 11 of 20 , Dec 1, 2006
• 0 Attachment
On Fri 1-Dec-06 7:19pm -0600, Yakov Lerner wrote:
> On 12/1/06, Bill McCarthy <WJMc@...> wrote:

>> One might think so, but also note:
>>
>> :help non-greedy

> I don't have tag 'non-greedy' in the help. I have vim 7.9.164
> from svn. Where do I get more updated helpfiles ?

I see Tony already provided the an approach for unix. To
update your directories on Windows (using 4nt):

copy /[!*~]/u/s "ftp://ftp.vim.org/pub/vim/runtime/dos" c:\vim\vim70

adjusting the target as appropriate. This copies only files
that are newer or didn't exist on the target. It skips
files ending with a tilde.

And if any help files were updated:

vim -nes "+helpt c:\vim\vim70\doc|q"

--
Best regards,
Bill
Your message has been successfully submitted and would be delivered to recipients shortly.