开发者

Regular expression that compares number of spaces with number in backreference?

开发者 https://www.devze.com 2023-04-04 18:01 出处:网络
I am trying to parse some reStructuredTex开发者_如何学编程t 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

I am trying to parse some reStructuredTex开发者_如何学编程t 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消