regex to match trailing whitespace, but not lines which are entirely whitespace (indent placeholders

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2010-04-19T15:45:40Z Indexed on 2010/04/19 15:53 UTC
Read the original article Hit count: 531

Filed under:
|
|

I've been trying to construct a ruby regex which matches trailing spaces - but not indentation placeholders - so I can gsub them out.

I had this /\b[\t ]+$/ and it was working a treat until I realised it only works when the line ends are [a-zA-Z]. :-( So I evolved it into this /(?!^[\t ]+)[\t ]+$/ and it seems like it's getting better, but it still doesn't work properly. I've spent hours trying to get this to work to no avail. Please help.

Here's some text test so it's easy to throw into Rubular, but the indent lines are getting stripped so it'll need a few spaces and/or tabs. Once lines 3 & 4 have spaces back in, it shouldn't match on lines 3-5, 7, 9.

some test test  
some test test      


  some other test (text)
  some other test (text)  
  likely here{ dfdf }
  likely here{ dfdf }        
  and this ;
  and this ;  

Alternatively, is there an simpler / more elegant way to do this?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about regex