[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: multi line regex's in vi ...



Adam Shand wrote:
> 
> > clever solution. wish i'd come up with that one.
> 
> ain't that always the way ... :)
> 
> > here's the lo-down:
> >
> > :g     <- globally--i.e. thru every line in the file
> 
> how is that different from :s/find/replace/g ?

ex is a LINE editor. if your line looks like this:
	life is like a box of chocolates
you can issue
	s/ /-/g
to turn it into
	life-is-like-a-box-of-chocolates
that is, s///=substitute on this line, /g=globally ON THIS LINE (all
instances get replaced).

whereas
	:g
denotes 'repeat on all LINES of the file'

so a ":g s///" will, on EACH LINE, replace the FIRST instance
of whatever; to replace all instances on each line you need
to use ":g s///g"

> > /^ *$/ <- for every line that is blank (zero or more spaces only)
> > +1     <- increment line counter (i.e. go to line following match!)
> 
> so this just moves it down a line right?  it finds the empty line and then
> goes down one.

right.

> > s/^ *  <- look for blanks at beginning of line
> > [^ ]/  <- that are followed by a non-blank
> > <p>    <- replace with a "<p>"
> > &/     <- and the string that was matched
> 
> so & is a short hand way of using ()'s and  \1?

tha's my guess (i'm not absolutely positive, but the documentation
will clarify that, i'm sure).

> > of course if you have two blank lines in a row, it breaks...
> 
> it'll just put the '<p>' at the beginning of the second blank line
> right?  is it possible to do two searches instead of
> incrementing the line counter?  ie. search for ^ *$ and then
> search for ^[^ ] so you are guarenteed to be at the next line of

no. on the s/// part, he had it look for non-blanks, which makes
it fail when it encounters a second blank line in a row:
	:g/^ *$/+1 s/^ *[^ ]/<p>&/
	                ^^^^
if you always have single blank lines (even with spaces on them)
this'll work, but it perishes when there's two in a row.

> > (probably better long-term solution is to use perl (or wml) to
> > filter your text.)
> 
> yeah, i'm checking out wml now, though with this solution i actually don't
> have a lot of need for one.

if you're just bringing a one-shot website from text to html,
then you're probably right. but if you do this kind of thing
often, either perl or wml (which uses perl, as i recall)
will save your bacon many times over...


Reply to: