简体   繁体   English

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

[英]match everything except the regex pattern

I have a xml content as below我有一个 xml 内容如下

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>

In the above i want to mach everything except 502 Bad Gateway in title.在上面,我想处理除标题中的502 Bad Gateway之外的所有内容。

I used below regex to match 502 Bad Gateway我使用下面的正则表达式来匹配502 Bad Gateway

(?<=title>)(.*?)(?=<\/title>)

Can someone tell me how to negate it?有人可以告诉我如何否定它吗?

I tried below suggestions我尝试了以下建议

(?!((?<=title>)(.*?)(?=<\/title>)))
[^((?<=title>)(.*?)(?=<\/title>))]

But they didn't worked.但他们没有奏效。

I am using ruby regex我正在使用 ruby 正则表达式

You want get everything that is not matched by the regEx?你想得到正则表达式不匹配的所有内容吗?

Save your result with the language do you prefer.使用您喜欢的语言保存结果。

Use some tools, for eg:使用一些工具,例如:

  • WinMerge WinMerge
  • Beyond Compare超越比较
  • KDiff3 KDiff3
  • DiffMerge差异合并
  • Merge合并
  • ... ...

Some of them could generate a patch.其中一些可以生成补丁。 Use File -> Open... to open the two versions for comparison.使用 File -> Open... 打开两个版本进行比较。 This will give you a nice view of what you have changed.这将使您对所做的更改有一个很好的了解。 https://docs.moodle.org/dev/How_to_create_a_patch#Creating_a_patch_using_WinMerge https://docs.moodle.org/dev/How_to_create_a_patch#Creating_a_patch_using_WinMerge

You could just use sub to remove '502 Bad Gateway' in title :您可以只使用sub删除title中的“502 Bad Gateway”:

xml = "<head><title>502 Bad Gateway</title></head>"
xml.sub("<title>502 Bad Gateway</title>", "<title></title>")
# => "<head><title></title></head>"

Not very familiar with ruby but I assume the features of regex are similar with PCRE, which supports control verbs and recursion.对 ruby 不太熟悉,但我认为正则表达式的功能与 PCRE 相似,它支持控制动词和递归。

Here's the regex that matches your description:这是与您的描述相匹配的正则表达式:

((?<=title>)502 Bad Gateway(?=<\/title>))(*SKIP)(*F)|(?:(?!(?1))[\s\S])+

See the proof证明

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

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