Regex对整条线的逆匹配

时间:2023-01-14 19:26:37

I've been trying to get this to work for an hour now but can't seem to do it, neither with the help of SO articles, or Regex101.com.

我已经试着让它工作了一个小时,但是似乎不能这么做,也没有SO文章或Regex101.com的帮助。

Have have some data and would like to return the lines that does not contain "/Common/http". Example data:

有一些数据,并希望返回不包含“/Common/http”的行。示例数据:

/Common/http and /Common/Another
/Common/http-mymon
/Common/another /Common/http
another line

The result I am looking for is:

我正在寻找的结果是:

/Common/http-mymon
another line

Any regex I use must match the whole line or it fails in the engine I use (https://github.com/jayway/JsonPath). This means that http would not work, but .*http.* would.

我使用的任何regex都必须匹配整个行,否则在我使用的引擎中失败(https://github.com/jayway/JsonPath)。这意味着http不能工作,但是。*http。*。

Hope this is fairly clear?

希望这很清楚?

/Patrik

/ Patrik

2 个解决方案

#1


2  

You can use a negative lookahead regex like this:

你可以像这样使用一个负面的前瞻regex:

^(?:.*?/Common/http-mymon|(?!.*/Common/http)).*$

RegEx Demo

RegEx演示


Update:

更新:

As per comment below OP wants to exclude /Common/http followed by / or a whitespace. In that case try this regex:

根据OP下面的注释,希望排除/公共/http,然后是/或空格。在这种情况下,尝试这个regex:

^(?!.*/Common/http(?:/|\s)).*$

RegEx Demo 2

RegEx演示2

#2


1  

I think you can go with the following regex

我想你可以使用下面的regex

^(?!.*\/Common\/http(?:\s))(.*?)$

This one is checking for no /Common/http in front before space using negative lookahead (?!.*\/Common\/http(?:\s))

这是在使用负的前视(?! *\/普通\/http(?:\s))在空格前检查no /Common/http

#1


2  

You can use a negative lookahead regex like this:

你可以像这样使用一个负面的前瞻regex:

^(?:.*?/Common/http-mymon|(?!.*/Common/http)).*$

RegEx Demo

RegEx演示


Update:

更新:

As per comment below OP wants to exclude /Common/http followed by / or a whitespace. In that case try this regex:

根据OP下面的注释,希望排除/公共/http,然后是/或空格。在这种情况下,尝试这个regex:

^(?!.*/Common/http(?:/|\s)).*$

RegEx Demo 2

RegEx演示2

#2


1  

I think you can go with the following regex

我想你可以使用下面的regex

^(?!.*\/Common\/http(?:\s))(.*?)$

This one is checking for no /Common/http in front before space using negative lookahead (?!.*\/Common\/http(?:\s))

这是在使用负的前视(?! *\/普通\/http(?:\s))在空格前检查no /Common/http