简体   繁体   English

正则表达式匹配除模式之外的所有内容

[英]Regex to match everything except a pattern

Regex noob here struggling with this, which I know it will be easy for some of you regex gods out there!正则表达式新手在这里苦苦挣扎,我知道你们中的一些正则表达式神很容易!

Given the following:鉴于以下情况:

title:      Some title
date:       2022-08-15
tags:       <value to extract>
identifier: 1234567
---------------------------

Some text
some more text

I would like a regex to match everything except the value of tags (ie the "<value to extract>" text).我想要一个正则表达式来匹配除tags值之外的所有内容(即“<value to extract>”文本)。

For context, this is supposed to run on emacs (in case it matters).对于上下文,这应该在 emacs 上运行(以防万一)。

EDIT: Just to clarify as per @phils question, all I care about extracting the tags value.编辑:只是根据@phils 的问题澄清一下,我只关心提取标签值。 However, this is via a package setting that asks for a regex string and I don't have much control over how it gets use.但是,这是通过要求正则表达式字符串的 package 设置,我无法控制它的使用方式。 It seems to expect a regex to strip what I don't need from the string rather than matching what I do want, which is slightly annoying.. Also, the since it seems to match everything with \\(.\\) , I'm guessing it's using the global flag?似乎期望一个正则表达式从字符串中去除我不需要的东西,而不是匹配我想要的东西,这有点烦人。而且,因为它似乎与\\(.\\)匹配所有内容,我我猜它正在使用全局标志?

Please let me know if any of this isn't clear.如果有任何不清楚的地方,请告诉我。

Emacs regular expressions can't trivially express "not foo" for arbitrary values of foo. Emacs 正则表达式不能简单地为 foo 的任意值表达“not foo”。 (The likes of PCRE have non-regular extensions for zero-width negative look-ahead/behind assertions, but in Emacs that sort of functionality is generally done with the support of lisp code 1 .) (PCRE 之类的对零宽度负前瞻/后置断言具有非常规扩展,但在 Emacs 中,这种功能通常是在 lisp 代码1的支持下完成的。)

You can still do it purely with regexp matching, but it's simply very cumbersome.您仍然可以纯粹使用正则表达式匹配来做到这一点,但这非常麻烦。 An Emacs regexp which matches any line which does not begin with tags: is:匹配任何tags:开头的行的 Emacs 正则表达式是:

^\(?:$\|[^t]\|t[^a]\|ta[^g]\|tag[^s]\|tags[^:]\).*


1 In lisp code you would instead simply check each line to see whether it does start with tags: and, if so, skip it (which is why Emacs generally gets away without the feature you're looking for, but of course that doesn't help you here). 1在 lisp 代码中,您只需检查每一行以查看它是否tags:如果是,则跳过它(这就是 Emacs 通常在没有您正在寻找的功能的情况下逃脱的原因,但当然不会t在这里帮助你)。

After playing around with it for a bit and taken inspiration from @phils' answer, I've come up with the following:在玩了一会儿并从@phils的回答中汲取灵感之后,我想出了以下几点:

"^\\(?:\\(#\\+\\)?\\(?:filetags:\s+\\|tags:\s+\\|title:.*\\|identifier:.*\\|date:.*\\)\\|.*\\)"

I've also added an extra \\(#\\+\\)?我还添加了一个额外的\\(#\\+\\)? to account for org meta keys which would usually have the format #+key: value .考虑到通常格式为#+key: value的 org 元键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM