regex - How to turn newlines into indents with regular expressions -


i have list looks this:

item 1   subitem 1  item 2  item 3   subitem 1   subitem 2    subsubitem 1  item 4 

pretty much, every top-level item has 1 newline before it, , each subitem has 2 newlines, , sub-subitems have three, , on. want in format similar this:

item 1     subitem 1 item 2 item 3     subitem 1     subitem 2         subsubitem 1 item 4 

the regex have been using in vim this:

for first level:

%s/^$\n\(\t\w\)/\t\1/g 

for second level:

%s/^$\n\(\t\t\w\)/\t\1/g 

and on.

what's better way without having run different regex each level of list? i'm trying use vim this, *nix solution fine me.

one thing can recursively use following regex :

(?<!\n)\n\t*\n 

recursively find , replace occurrence of regex

  • first pass replace : \n
  • second pass replace : \n\t
  • third pass replace : \n\t\t
  • fourth pass replace : \n\t\t\t

...and on until there no match regex anywhere.

so don't have run different regex every time, still you'll have change replace part. can write small program recursively it.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -