简体   繁体   English

正则表达式匹配不以==开头的所有行

[英]Regex to match all lines not starting with ==

This should be simple, but I've been having trouble with it. 这应该很简单,但是我遇到了麻烦。 I want to write a regex in perl to match all lines that do not begin with an "==". 我想在perl中写一个正则表达式来匹配所有不以“ ==”开头的行。 I created this expression: 我创建了这个表达式:

^[^\\=\\=].* ^ [^ \\ = \\ =]。*

Which works fine in a regex tester I use, but when I run the perl script I get an error stating: 在我使用的正则表达式测试器中可以正常工作,但是当我运行perl脚本时,出现错误消息:

POSIX syntax [= =] is reserved for future extensions in regex POSIX语法[= =]保留用于正则表达式中的将来扩展

And the script terminates. 脚本终止。 I assume I'm using some syntax wrong, but I haven't found anything regarding this. 我以为我使用的语法错误,但是我对此一无所获。 Does anyone have a better way to match these lines? 有谁有更好的方法来匹配这些线?

Your regex is incorrect, as it fails with =A as input, by example. 您的正则表达式是不正确的,例如,由于它以=A作为输入而失败。 A way to do it would be with a Perl Compatible Regular Expression(PCRE): ^(?!==) 一种方法是使用Perl兼容的正则表达式(PCRE): ^(?!==)

You're misunderstanding how character classes work in regular expressions 您误会了字符类在正则表达式中的工作方式

A character class is delimited by square brackets [...] and generally will match any one of the characters that it encloses. 字符类是用方括号分隔[...]一般将匹配它包围的任何一个字符。 So [abc] will match a , b , or c , but only the first character of aa or cbc . 因此[abc]将匹配abc ,但仅匹配aacbc的第一个字符。 You probably know that you can also use ranges , such as [ac] 您可能知道您也可以使用range ,例如[ac]

You can also negate the class, as you have done, so [^a] will match any one character that isn't an a , such as z or & , but only the first character of zz 您也可以像这样对否定类进行否定 ,因此[^a]将匹配不是 a任何字符,例如z& ,但仅匹配zz的第一个字符

Replicating a character in a class will not change what it matches, so [aardvark] will match exactly one of a , d , k , r , or v , and is equivalent to [adkrv] 在类中复制字符不会更改其匹配项,因此[aardvark]将完全匹配adkrv ,并且等效于[adkrv]

Your regex pattern uses the character class [^\\=\\=] . 您的正则表达式模式使用字符类[^\\=\\=] It's unnecessary to escape an equals sign, and replicating it has no effect, so you have the equivalent of [^=] , which will match any single character other than the equals sign = 不必转义等号,并且对其进行复制没有任何作用,因此您具有等价的[^=] ,它将匹配等号=以外的任何单个字符

The reason you got that error message is that character classes beginning [= and ending =] (just [=] doesn't count) are reserved for special behaviour yet to be implemented. 收到该错误消息的原因是,字符类以[=和结束=] (只是[=]不计算在内)保留给尚未实现的特殊行为。 As above, there would ordinarily be no reason to write a character class with multiple occurrences of the same character, so it's reasonable to disallow such a construction 如上所述,通常无需编写多次出现相同字符的字符类,因此合理的做法是禁止这样的构造

perldoc perldiag has this to say perldoc perldiag有这个话要说

POSIX syntax [= =] is reserved for future extensions in regex; POSIX语法[= =]保留用于正则表达式中的将来扩展。 marked by <-- HERE in m/%s/ 以<-HERE标记,以m /%s /

(F) Within regular expression character classes ([]) the syntax beginning with "[=" and ending with "=]" is reserved for future extensions. (F)在正则表达式字符类([])中,以“ [=”开头和以“ =]结尾”的语法保留给以后的扩展。 If you need to represent those character sequences inside a regular expression character class, just quote the square brackets with the backslash: "[=" and "=]". 如果需要在正则表达式字符类中表示这些字符序列,只需在方括号中加上反斜杠:“ [=“和” =]“。 The <-- HERE shows whereabouts in the regular expression the problem was discovered. <-HERE在正则表达式中显示发现问题的行踪。 See perlre . 参见perlre

A solution depends on how you want to use the test in your Perl code, but if you need an if statement then I would simply invert the test and check that the line doesn't start with == 一个解决方案取决于您想在Perl代码中使用测试的方式,但是如果您需要if语句,那么我将简单地反转测试并检查该行不是==开头

unless ( /^==/ )

or, if you're allergic to Perl's unless 或者,如果您对Perl过敏, unless

if ( not /^==/ )

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

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