Using regex to match string between two strings while excluding strings

后端 未结 6 1888
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 19:41

Following on from a previous question in which I asked:

How can I use a regular expression to match text that is between two strings, where those two

6条回答
  •  佛祖请我去吃肉
    2020-12-06 20:41

    A better question to ask yourself than "how do I do this with regular expressions?" is "how do I do solve this problem?". In other words, don't get hung up on trying to solve a big problem with regular expressions. If you can solve half the problem with regular expressions, do so, then solve the other half with another regular expression or some other technique.

    For example, make a pass over your data getting all matches, ignoring the unwanted text (read: get results both with and without the unwanted text). Then, make a pass over the reduced set of data and weed out those results that have the unwanted text. This sort of a solution is easier to write, easier to understand and easier to maintain over time. And for any problem you're likely to need to solve with this approach it will be sufficiently fast enough.

提交回复
热议问题