RegEx: can't figure out the expression to match lines with individual events and match only those containing certain word

╄→гoц情女王★ 提交于 2019-12-06 01:45:36

I think you need to add a lookahead to prevent it from going beyond the boundary:

BEGIN:VEVENT([\s\S](?!BEGIN:VEVENT))+?Birthday[\s\S]+?END:VEVENT

NB: I'm not a ST user, no idea if it supports that.

Firstly: if I were doing this, and especially if it started getting any more complicated, I'd whip up a quick Perl/Python/etc script to filter through things. That will be much more powerful and flexible, and less finicky. RegEx isn't great at this kind of thing.

That said, you can get this done with RegEx alone, although it's messy. What you need to do is prevent the END lines from being included in your "middle section". To accomplish that, you can do this if Sublime doesn't support lookaheads:

BEGIN:VEVENT\n(([^E]|E[^N]|EN[^D]).*\n)*(([^E]|E[^N]|EN[^D]).*Birthday.*\n)(([^E]|E[^N]|EN[^D]).*\n)*END:VEVENT\n

Expanded a bit:

BEGIN:VEVENT\n
(([^E]|E[^N]|EN[^D]).*\n)*           //Any number of non-END lines
(([^E]|E[^N]|EN[^D]).*Birthday.*\n)  //At least one Birthday line
(([^E]|E[^N]|EN[^D]).*\n)*           //More non-END lines
END:VEVENT\n

And technically, you could also exclude the END bits on the Birthday line, since END:VEVENT will never contain "Birthday" anyway.

Again, that's super messy, I would recommend the lookahead solution above, or a custom script if things get more complicated. But I worked this one out, so I figured I'd post it anyway. Maybe show it to your kids to give them a good scare or something.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!