Re: Regex Help - Greedy vs. Non-Greedy
On Wed, Sep 09, 2009 at 09:15:25AM -0700, Drew Tomlinson wrote: > I'm trying to do a search and replace in vim. I have lines like this: > http://site1/dir/; > http://site2/dir/;LastName, FirstName;Phone; > http://site3/dir/;LastName, FirstName; > http://site4/dir/; > > I'm want to match "http:*" and stop matching at the first ";". My basic > regex is: > > /http:.\+;/ > > But it's matching *all* the semi-colons. Thus I've Googled and tried > various incatations to try and make my regex "non-greedy" but I can't > seem to come up with the correct combination. LOL. Do yourself a favour and stop "Googling". Vim has a built-in help system. To access help for regular expressions: :help regexp :he regexp :he r[TAB] ... and search for non-greedy (or just scroll down). > How can I write a regex that stops matching at the first semi-colon? You've already received a few answers, but I'll add one that may be even better -- rely on external programs instead. Vim can be a bit clunky at times. The simplest and most typical usage would be :[range] !command If using Perl $ perldoc -h $ perldoc -q regex -- George ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: Regex Help - Greedy vs. Non-Greedy
On Wed, Sep 09, 2009 at 09:15:25AM -0700, Drew Tomlinson wrote: > I'm trying to do a search and replace in vim. I have lines like this: > http://site1/dir/; > http://site2/dir/;LastName, FirstName;Phone; > http://site3/dir/;LastName, FirstName; > http://site4/dir/; > > I'm want to match "http:*" and stop matching at the first ";". My basic > regex is: > > /http:.\+;/ Use "{-}" in place of "+". /http:.\{-};/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: Regex Help - Greedy vs. Non-Greedy
Drew Tomlinson wrote: Daniel Bye wrote: On Wed, Sep 09, 2009 at 09:15:25AM -0700, Drew Tomlinson wrote: I'm trying to do a search and replace in vim. I have lines like this: http://site1/dir/; http://site2/dir/;LastName, FirstName;Phone; http://site3/dir/;LastName, FirstName; http://site4/dir/; I'm want to match "http:*" and stop matching at the first ";". My basic regex is: /http:.\+;/ But it's matching *all* the semi-colons. Thus I've Googled and tried various incatations to try and make my regex "non-greedy" but I can't seem to come up with the correct combination. How can I write a regex that stops matching at the first semi-colon? Tested in vi, not vim: /http:[^;]*/ Dan Thanks for your reply. I tried it in vim (or more specifically, gvim 7.2) and your example matches all semi-colons. However in vi, it does stop at the first semi-colon as you say. Can anyone please explain the difference? Thanks, Drew Never mind. My mistake. The above does work in gvim. I had a "." (dot) after http:. Thanks Mel & Dan! Cheers, Drew -- Be a Great Magician! Visit The Alchemist's Warehouse http://www.alchemistswarehouse.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: Regex Help - Greedy vs. Non-Greedy
Daniel Bye wrote: On Wed, Sep 09, 2009 at 09:15:25AM -0700, Drew Tomlinson wrote: I'm trying to do a search and replace in vim. I have lines like this: http://site1/dir/; http://site2/dir/;LastName, FirstName;Phone; http://site3/dir/;LastName, FirstName; http://site4/dir/; I'm want to match "http:*" and stop matching at the first ";". My basic regex is: /http:.\+;/ But it's matching *all* the semi-colons. Thus I've Googled and tried various incatations to try and make my regex "non-greedy" but I can't seem to come up with the correct combination. How can I write a regex that stops matching at the first semi-colon? Tested in vi, not vim: /http:[^;]*/ Dan Thanks for your reply. I tried it in vim (or more specifically, gvim 7.2) and your example matches all semi-colons. However in vi, it does stop at the first semi-colon as you say. Can anyone please explain the difference? Thanks, Drew -- Be a Great Magician! Visit The Alchemist's Warehouse http://www.alchemistswarehouse.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: Regex Help - Greedy vs. Non-Greedy
On Wednesday 09 September 2009 18:15:25 Drew Tomlinson wrote: > I'm trying to do a search and replace in vim. I have lines like this: > http://site1/dir/; > http://site2/dir/;LastName, FirstName;Phone; > http://site3/dir/;LastName, FirstName; > http://site4/dir/; > > I'm want to match "http:*" and stop matching at the first ";". My basic > regex is: > > /http:.\+;/ > > But it's matching *all* the semi-colons. Thus I've Googled and tried > various incatations to try and make my regex "non-greedy" but I can't > seem to come up with the correct combination. > > How can I write a regex that stops matching at the first semi-colon? AFAIK, there's no greediness modifier in vim regex. However, you can use character classes to solve your problem: %s/http:[^;]\+/foo/g -- Mel ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"
Re: Regex Help - Greedy vs. Non-Greedy
On Wed, Sep 09, 2009 at 09:15:25AM -0700, Drew Tomlinson wrote: > I'm trying to do a search and replace in vim. I have lines like this: > http://site1/dir/; > http://site2/dir/;LastName, FirstName;Phone; > http://site3/dir/;LastName, FirstName; > http://site4/dir/; > > I'm want to match "http:*" and stop matching at the first ";". My basic > regex is: > > /http:.\+;/ > > But it's matching *all* the semi-colons. Thus I've Googled and tried > various incatations to try and make my regex "non-greedy" but I can't > seem to come up with the correct combination. > > How can I write a regex that stops matching at the first semi-colon? Tested in vi, not vim: /http:[^;]*/ Dan -- Daniel Bye _ ASCII ribbon campaign ( ) - against HTML, vCards and X - proprietary attachments in e-mail / \ pgphr4aSkzZCA.pgp Description: PGP signature