I have a pattern:
“\nvariable WORD”
This pattern shows up a lot of times in the string and I want a list of indexes that this pattern shows up at. “WORD” is fixed, and doesn’t change from instance to instance, but “variable” varies in content and length.
In python, I know this matches all WORD and returns their indices in a list:
contents="some long string"
print [m.start() for m in re.finditer('WORD',contents)]
So in short, how do I find indices of all “variable” after \n but before “WORD”?
If the only tool you know is a hammer, every problem looks like a nail.
Regular expressions are powerful hammers, but sometimes not the best tool for the task in hand. In fact, regular expressions are abused a lot, I feel shivers down the spine every time someone asks me to check complex regular expressions from other programmer (often I’m unable understand mine after a few weeks).
On the other side, EBNF (Extended Backus–Naur Form) notation is a lot easier to understand and maintain.
This will print: