by somat · 3y ago · 0 comments
The standard unix regular expression implementations (grep, sed, ed, vi) are very tied to "lines" of text, and most of the time this is exactly what you want. but sometimes it gets in the way. And when you start to have trouble with your regex because it is conflicting with line returns, then it is the sam editor and it's structural regex engines time to shine. They would probably be great elsewhere, but nobody else uses them.
look for the section on structural expressions.
I am not really an expert but my understanding is that they are called that because you can use them to define the structure of a match. in most regular expression engines the structure is defined as a line of text or the whole document and the expression only matches the contents. This is why they do so well with multi line records you just need to define a structure expression that matches your, well.. structure.
I never did learn sam all that well, I mainly used it as a non interactive sed replacement. however I just learned that apparently the vis project uses structural expressions. I might try it. Mainly because at least then my fingers sort of know what to do.
That definitely happens, but 9 times out of 10, you can add /m to the end of the regex, and now ^ and $ match individual lines, while \A and \Z match the entire string.
That being said, I've been recently employing comby (https://comby.dev/) in my workflow, which solves similar problem, but understands certain languages to simplify the usage.