简体   繁体   中英

match everything except the regex pattern

I have a xml content as below

<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.

I used below regex to match 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

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
  • Beyond Compare
  • KDiff3
  • DiffMerge
  • Merge
  • ...

Some of them could generate a patch. Use File -> Open... to open the two versions for comparison. 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

You could just use sub to remove '502 Bad Gateway' in title :

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.

Here's the regex that matches your description:

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

See the proof

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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