正则表达式排除行? [英] Regular expression to exclude lines?

查看:219
本文介绍了正则表达式排除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,问一下这是一个多么微不足道的问题。也很遗憾,我手头没有现在的代码版本,但是......不管怎么说,一定有些问题

试图做负面的。每当我试图处理正则表达式时,我似乎都会陷入这些困境。


所有我想做的就是删除这些行它不包含特定的

字符串。实际上是一个用于编辑日志文件的过滤器。我可以用null找到并替换一个

的东西,但是无法弄清楚如何找到不包含

的东西。


更进一步,我想概括并使用包含

决策字符串的JavaScript变量,但首先我要担心不在线内

问题。

Sorry to ask what is surely a trivial question. Also sorry that I don''t have
my current code version on hand, but... Anyway, must be some problem with
trying to do the negative. It seems like I get into these ruts each time I
try to deal with regular expressions.

All I''m trying to do is delete the lines which don''t contain a particular
string. Actually a filter to edit a log file. I can find and replace a thing
with null, but can''t figure out how to find the lines which do not contain
the thing.

Going further, I want to generalize and use a JavaScript variable containing
the decision string, but first I need to worry about the not-within-a-line
problem.

推荐答案

Shannon Jacobs写道:
Shannon Jacobs wrote:
抱歉问什么是一个微不足道的问题。


嗯,我不认为这是微不足道的。

所有我想做的就是删除不行的行包含
特定字符串。实际上是一个用于编辑日志文件的过滤器。我可以用null找到并替换一个东西,但是不知道如何找到不包含该东西的行。
Sorry to ask what is surely a trivial question.
Hm, I don''t think it is this trivial.
All I''m trying to do is delete the lines which don''t contain a
particular string. Actually a filter to edit a log file. I can
find and replace a thing with null, but can''t figure out how to
find the lines which do not contain the thing.




这里是一个快速的过滤器,过滤掉三行中

不包含line字样的行:


alert(") ;这是一条线。它是一个\这是一个

行" .match(/ \ n * [^ \ n] * \ n *([^ \ n ] * [^ l] [^ i] [^ n] [^ e] [^ \ n] * \ n)* [^ \ n] * \ n * /)[1])


但是必须有一个更好的更好的方式,IIRC有一些东西

称为负向前瞻,由JavaScript 1.5支持,

我还没有合作过。

PointedEars



Here''s a quickhack that filters out of three lines the one that
does not contain the word `line'':

alert("this is a line\nthis is a\nthis is a
line".match(/\n*[^\n]*\n*([^\n]*[^l][^i][^n][^e][^\n]*\n)*[^\n]*\n*/)[1])

But there must be a better a better way, IIRC there is something
called `negative lookahead'', supported from JavaScript 1.5 on,
which I have yet not worked with.
PointedEars


托马斯''PointedEars''Lahn< Po ****** ***@web.de>写道:
Thomas ''PointedEars'' Lahn <Po*********@web.de> writes:
Shannon Jacobs写道:
Shannon Jacobs wrote:
很抱歉,问一下这是一个多么微不足道的问题。
嗯,我不喜欢我认为这是微不足道的。
Sorry to ask what is surely a trivial question.
Hm, I don''t think it is this trivial.




我也没有。正则表达式中的负面匹配很少。

这里是快速过滤的过滤器在三行中,
不包含line一词:

警告(这是一行',这是一个\这是一个 line" .match(/ \ n * [^ \ n] * \ n *([^ \ n] * [^ l] [^ i] [^ n] [^ e] [^ \ n] * \ n)* [^ \ n] * \ n * /)[1])$ ​​b $ b

这纯粹是偶然的。如果你在前面添加一行,例如,

bad thing\\\
t这是一行,这是一个这是一条线,它匹配

包含第二行和第三行的字符串。

但是必须有一个更好的更好的方法,IIRC有一些叫做负向前瞻的东西,从JavaScript 1.5开始支持,
我还没有合作过。



Neither do I. Negative matches in regular expressions rarely are.
Here''s a quickhack that filters out of three lines the one that
does not contain the word `line'':

alert("this is a line\nthis is a\nthis is a
line".match(/\n*[^\n]*\n*([^\n]*[^l][^i][^n][^e][^\n]*\n)*[^\n]*\n*/)[1])
That''s purely accidental. If you add a line in front, e.g.,
"bad thing\nthis is a line\nthis is a\n this is a line", it matches
the string containing the second and third line.
But there must be a better a better way, IIRC there is something
called `negative lookahead'', supported from JavaScript 1.5 on,
which I have yet not worked with.




否定前瞻可能是一种更简单的方法。


艰难的方式:


/ ^([^ l\ n] *(l [^ i] | li [^ n] | lin [^ e]))*( [^ l\ n])*



Negative lookahead might be an easier way to do it.

The hard way:

/^([^l\n]*(l[^i]|li[^n]|lin[^e]))*([^l\n])*


/ m

(任何l后面跟不是ine)


负向前瞻:

/ ^([^ l\\\
] * l(?!ine))* [^ l\ n] *
/m
(any "l" is not followed by "ine")

With negative lookahead:
/^([^l\n]*l(?!ine))*[^l\n]*


这篇关于正则表达式排除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆