0

I am trying to parse some reStructuredText and want to be able to identify when the indent level has changed. So, I need to be able to see when an indent of 8 spaces has changed to an indent of 4 spaces (for example), so that I can change the color of that text block. Is there a way of using regular expressions to count the number of spaces in the indent and pick out the next line that contains a shallower indent?

Chris Fonnesbeck
  • 4,143
  • 4
  • 29
  • 30
  • If you want a parser, write a parser. – Carl Norum Sep 14 '11 at 20:49
  • Even with a proper parser _reStructuredText_ is a real pain to parse due to its incredible ambiguous rules. Also see: http://stackoverflow.com/questions/6178546/antlr-grammar-for-restructuredtext-rule-priorities – Bart Kiers Sep 14 '11 at 20:58

1 Answers1

0

Something like this will work:

/
^(\s*)\S.*$    #Find a line with some number of spaces
(?:^\1\S.*$)*  #Find more lines with the same starting spaces
^.*$           #This is the line you want here
/xm            #x to ignore whitespace in the regex.
               #m to have ^and $ match all lines
Jacob Eggers
  • 9,062
  • 2
  • 25
  • 43
  • Thanks for the suggestion. It does not seem to be able to count the number of spaces, however. So, if I have the first line indented by 4, then several indented by 8, then another line indented by 4 again, it does not seem to be able to tell the difference. – Chris Fonnesbeck Sep 16 '11 at 14:39