How to recognize a single line via regexs
Hi, how can I identify a single line no longer than e.g. 60 characters preceded and followed by a blank line via regexs. This way I want to identify section headings. What I did was mark every blank line with %s/^$// and than chomp the CR %s/\n//g and if the text between the 's isn't longer than 60 characters put it into \section{}, and replace every with \r\r. But in larger files this takes a while. Is there a smarter solution to the problem? Thanks in advance Chris
Re: How to recognize a single line via regexs
Informationen wrote: Hi, how can I identify a single line no longer than e.g. 60 characters preceded and followed by a blank line via regexs. This way I want to identify section headings. What I did was mark every blank line with %s/^$// and than chomp the CR %s/\n//g and if the text between the 's isn't longer than 60 characters put it into \section{}, and replace every with \r\r. But in larger files this takes a while. Is there a smarter solution to the problem? The following regexp will do the match: ^\n\zs.\{1,60}\ze\n$ ^ beginning of line \n newline \zs pattern really starts here (but must be precededed by the foregoing) .\{1,60} one to sixty characters \ze pattern really ends here (but must be followed by the trailing pattern) \n newline $ end-of-line Please read: :help regexp for (much) more. Regards, Chip Campbell
Re: How to recognize a single line via regexs
how can I identify a single line no longer than e.g. 60 characters preceded and followed by a blank line via regexs. This way I want to identify section headings. What I did was mark every blank line with %s/^$// and than chomp the CR %s/\n//g and if the text between the 's isn't longer than 60 characters put it into \section{}, and replace every with \r\r. But in larger files this takes a while. Is there a smarter solution to the problem? You might try: /^\s*\n\zs.\{1,60}\ze\n\s*$ /^\n\zs.\{1,60}\ze\n$ which should find what you describe (the former allows a blank line to contain whitespace while the latter looks for a truely blank line with zero whitespace). It breaks down as ^ the start of line \s*(the optional whitespace, as described) \n a newline \zstreat this as the beginning of the match .\{1,60} 1-60 non-newline characters \zeyou could optionally use this here to treat this as the end of the match \n another newline \s*(more optional whitespace, as described) $ the end of a line The above regexp(s) (with or without the \s* or the \ze) can also be used in :s commands to do things like :%s/^\s*\n\zs.\{1,60}\ze\n\s*$/\U to uppercase all your titles, or :%s/^\s*\n\zs.\{1,60}\ze\n\s*$/\=submatch(0).\n.substitute(submatch(0), '.', '=', 'g') which will underline all your hits with a row of = the same length as your section-title. Just a few ideas, -tim