Help with \(-) or Greedy behaviors

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

Help with \(-) or Greedy behaviors

Hopkins, Jason (GE Healthcare)
I'm having a hard time understanding how to deal with greedy behavior in
Vim.
 
I'm trying to search through an html and find the stuff between tags
">stuff<".
 
>.\+< is too greedy for me.
>.\{-}< will also pick up >< I only want to find tags with something in
them.

/>.*</{-} changes to >.*< which is greedy again.

/>.*</{1,} changes to >.*<.

It appears anything after < gets ignored.
 
 
Thanks,
Jason
Reply | Threaded
Open this post in threaded view
|

RE: Help with \(-) or Greedy behaviors

jason heddings-2
Have you tried:
>[^>]*<

HTH,
--jah

-----Original Message-----
From: Hopkins, Jason (GE Healthcare) [mailto:[hidden email]]
Sent: Tuesday, 04 October, 2005 10:01
To: [hidden email]
Subject: Help with \(-) or Greedy behaviors

I'm having a hard time understanding how to deal with greedy behavior in
Vim.
 
I'm trying to search through an html and find the stuff between tags
">stuff<".
 
>.\+< is too greedy for me.
>.\{-}< will also pick up >< I only want to find tags with something in
them.

/>.*</{-} changes to >.*< which is greedy again.

/>.*</{1,} changes to >.*<.

It appears anything after < gets ignored.
 
 
Thanks,
Jason

Reply | Threaded
Open this post in threaded view
|

RE: Help with \(-) or Greedy behaviors

jason heddings-2
In reply to this post by Hopkins, Jason (GE Healthcare)
Sorry, didn't read quite carefully enough.
To make sure something is in the tag:

>[^>]+<

--jah


-----Original Message-----
From: jason heddings [mailto:[hidden email]]
Sent: Tuesday, 04 October, 2005 10:10
To: 'Hopkins, Jason (GE Healthcare)'; '[hidden email]'
Subject: RE: Help with \(-) or Greedy behaviors

Have you tried:
>[^>]*<

HTH,
--jah

-----Original Message-----
From: Hopkins, Jason (GE Healthcare) [mailto:[hidden email]]
Sent: Tuesday, 04 October, 2005 10:01
To: [hidden email]
Subject: Help with \(-) or Greedy behaviors

I'm having a hard time understanding how to deal with greedy behavior in
Vim.
 
I'm trying to search through an html and find the stuff between tags
">stuff<".
 
>.\+< is too greedy for me.
>.\{-}< will also pick up >< I only want to find tags with something in
them.

/>.*</{-} changes to >.*< which is greedy again.

/>.*</{1,} changes to >.*<.

It appears anything after < gets ignored.
 
 
Thanks,
Jason

Reply | Threaded
Open this post in threaded view
|

RE: Help with \(-) or Greedy behaviors

Keith W. Roberts
In reply to this post by Hopkins, Jason (GE Healthcare)
----Original Message----
From: Hopkins, Jason (GE Healthcare)
[mailto:[hidden email]]
Sent: Tuesday, October 04, 2005 9:01 AM
To: [hidden email]
Subject: Help with \(-) or Greedy behaviors

> I'm having a hard time understanding how to deal with greedy behavior
> in Vim.
>
> I'm trying to search through an html and find the stuff between tags
> ">stuff<".
>
>> .\+< is too greedy for me.
>> .\{-}< will also pick up >< I only want to find tags with
> something in
> them.
>
> />.*</{-} changes to >.*< which is greedy again.
>
> />.*</{1,} changes to >.*<.
>
> It appears anything after < gets ignored.
>
>
> Thanks,
> Jason

Search for >[^>]*<      ... that is,
> followed by anything BUT >, up until the next < encountered.  Use \zs and
\ze to make the search exclusive, or \( and \) to grab "stuff" as \1:

>\zs[^>]*\ze<
>\([^>]*\)<

-Keith

Reply | Threaded
Open this post in threaded view
|

RE: Help with \(-) or Greedy behaviors

jason heddings-2
In reply to this post by jason heddings-2
Sheesh, I need to slow down and type slower...
>[^>]\+<



-----Original Message-----
From: jason heddings [mailto:[hidden email]]
Sent: Tuesday, 04 October, 2005 10:11
To: 'Hopkins, Jason (GE Healthcare)'; [hidden email]
Subject: RE: Help with \(-) or Greedy behaviors

Sorry, didn't read quite carefully enough.
To make sure something is in the tag:

>[^>]+<

--jah


-----Original Message-----
From: jason heddings [mailto:[hidden email]]
Sent: Tuesday, 04 October, 2005 10:10
To: 'Hopkins, Jason (GE Healthcare)'; '[hidden email]'
Subject: RE: Help with \(-) or Greedy behaviors

Have you tried:
>[^>]*<

HTH,
--jah

-----Original Message-----
From: Hopkins, Jason (GE Healthcare) [mailto:[hidden email]]
Sent: Tuesday, 04 October, 2005 10:01
To: [hidden email]
Subject: Help with \(-) or Greedy behaviors

I'm having a hard time understanding how to deal with greedy behavior in
Vim.
 
I'm trying to search through an html and find the stuff between tags
">stuff<".
 
>.\+< is too greedy for me.
>.\{-}< will also pick up >< I only want to find tags with something in
them.

/>.*</{-} changes to >.*< which is greedy again.

/>.*</{1,} changes to >.*<.

It appears anything after < gets ignored.
 
 
Thanks,
Jason


Reply | Threaded
Open this post in threaded view
|

Re: Help with \(-) or Greedy behaviors

Tim Chase-2
In reply to this post by Hopkins, Jason (GE Healthcare)
>>.\+< is too greedy for me.
>>.\{-}< will also pick up >< I only want to find tags with something in

Sounds like you want

        >.\{-1,}<

or alternatively

        >[^<]\+<

See

        :help /\{-

and read the line on "\{-n,}" for more on non-greedy matching
with a minimum count.

Additionally, if you want to do replacements on just the
contents, you can tag them with \(...\) and reference them later
as "\1", such as

        :%s!>\([^<]\+\)<!>before \1 after<!g

which will put "before" and "after" around the contents of tags.

HTH,

-tim









Reply | Threaded
Open this post in threaded view
|

Re: Help with \(-) or Greedy behaviors

A.J.Mechelynck
In reply to this post by Hopkins, Jason (GE Healthcare)
Hopkins, Jason (GE Healthcare) wrote:

> I'm having a hard time understanding how to deal with greedy behavior in
> Vim.
>  
> I'm trying to search through an html and find the stuff between tags
> ">stuff<".
>  
>> .\+< is too greedy for me.
>> .\{-}< will also pick up >< I only want to find tags with something in
> them.
>
> />.*</{-} changes to >.*< which is greedy again.
>
> />.*</{1,} changes to >.*<.
>
> It appears anything after < gets ignored.
>  
>  
> Thanks,
> Jason
>
>
>
>
 * will match zero or more, as many as possible, until the last
occurrence in the file of whatever follows.
 {-} will match as few as possible, possibly zero.
If you want something nonempty between > and < then say it, for instance:

        >[^<]\+<

which means
        greater-than
        one or more (as many as possible) other than less-than
        less-than

However you might want to match an opening tag with its own closing tag
and not just any less-than character. I believe this pattern (untested)
might do it:

        /<\(\k\{-1,}\>\).\{-}>.\{-1,}<\/\1\s{-}>/
which matches:
        < less-than
        \( start of what will be used as \1 (the tag name)
        \k\{-1,} at least one keyword character (see 'iskeyword'), as few as
possible
        \> end-of-word
        \) close \(
        .\{-} zero or more of anything, as few as possible
        > greater-than
        .\{-1,} one or more of anything, as few as possible
        < less-than
        \/ slash
        \1 the tag name we marked off by \( and \)
        \s{-} zero or more whitespace characters, as few as possible
        > greater-than


Best regards,
Tony

Reply | Threaded
Open this post in threaded view
|

RE: Help with \(-) or Greedy behaviors

Hopkins, Jason (GE Healthcare)
In reply to this post by Hopkins, Jason (GE Healthcare)
Thanks Tim

>[^<]\+< works just fine.



-----Original Message-----
From: Tim Chase [mailto:[hidden email]]
Sent: Tuesday, October 04, 2005 11:28 AM
To: Hopkins, Jason (GE Healthcare)
Cc: [hidden email]
Subject: Re: Help with \(-) or Greedy behaviors

>>.\+< is too greedy for me.
>>.\{-}< will also pick up >< I only want to find tags with something in

Sounds like you want

        >.\{-1,}<

or alternatively

        >[^<]\+<

See

        :help /\{-

and read the line on "\{-n,}" for more on non-greedy matching with a
minimum count.

Additionally, if you want to do replacements on just the contents, you
can tag them with \(...\) and reference them later as "\1", such as

        :%s!>\([^<]\+\)<!>before \1 after<!g

which will put "before" and "after" around the contents of tags.

HTH,

-tim