如果没有特定的模式,我如何匹配所有的元素?

时间:2022-09-13 12:20:36

I have strings in a format like this. I want to match strings that don't have abcd at the start.

我有这样格式的字符串。我想匹配一开始没有abcd的字符串。

abcd.efgh.ijkl
pqrs.efgh.ijkl
xyz.efgh.ijkl

I have come up with this expression (?<!abcd).efgh.ijkl http://rubular.com/r/jyZMIJxoNz

我想到了这个表达(?

It kinda does what I need. It matches the .efgh.ijkl part of pqrs.efgh.ijkl and xyz.efgh.ijkl and ignores abcd.efgh.ijkl. But I also want it to match the pqrs and xyz parts.

它能满足我的需要。它与.efgh匹配。ijkl pqrs.efgh的一部分。ijkl xyz.efgh。ijkl并忽略abcd.efgh.ijkl。但我也希望它与pqrs和xyz部件匹配。

I tried making a conditional like this (?(?<!abcd)|.*\.efgh.ijkl) but it's not even being recognized as a regex. What's wrong with the syntax? Does it not say "If it starts with abcd then blank else match everything up to .efgh.ijkl?

我尝试过这样的条件(?

5 个解决方案

#1


2  

[^\s]*(?<!abcd).efgh.ijkl

http://rubular.com/r/h11pUhuYSD

http://rubular.com/r/h11pUhuYSD

Should work for your purposes. It even matches if the target is in a longer string.

应该为你的目的工作。如果目标在较长的字符串中,它甚至可以匹配。

#2


1  

You want to use a lookahead for this, not a lookbehind.

你需要使用一个前视,而不是后视。

^(?!abcd\.)[a-z]+(?:\.[a-z]+)+$

The main regex is ^[a-z]+(?:\.[a-z]+)+$, which matches a string consisting of two or more clumps of letters separated by dots. The lookahead right after the start anchor ensures that the first clump is not abcd.

主regex ^[a - z]+(?:\[a - z]+)+ $匹配字符串组成的两个或两个以上的字母来表示。开始锚点之后的前视确保第一个块不是abcd。

Be aware that, if it's really Ruby you're doing this in, ^ and $ are line anchors. That means the regex would pluck the second line out of the string:

请注意,如果是真的Ruby你这样做,^和$行锚。这意味着regex将从字符串中提取第二行:

foo
pqrs.efgh.ijkl
bar

...which might not be what you want. To make sure you only match whole strings in Ruby, you should use the string anchors, \A and \z:

…这可能不是你想要的。为了确保在Ruby中只匹配整个字符串,您应该使用字符串锚,\A和\z:

\A(?!abcd\.)[a-z]+(?:\.[a-z]+)+\z

As for your attempt to use conditionals, Ruby doesn't seem to support them. But it doesn't matter, that wouldn't have worked anyway.

至于您尝试使用条件语句,Ruby似乎不支持它们。但没关系,反正那也没用。

#3


0  

Try this:

试试这个:

(?m)^(?!abcd).+$

(?)^(? ! abcd)+ $

Explanation:

解释:

<!--
(?m)^(?!abcd).+$

Options: ^ and $ match at line breaks

Match the remainder of the regex with the options: ^ and $ match at line breaks (m) «(?m)»
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!abcd)»
   Match the characters “abcd” literally «abcd»
Match any single character that is not a line break character «.+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
-->

#4


0  

Try this one:

试试这个:

[^"(a.+b)|(b.+c)|(c.+d)|"].* 

http://rubular.com/r/51OShSXwUz

http://rubular.com/r/51OShSXwUz

#5


0  

Negative lookbehinds are fun and they're a good tool to use.

负面的相貌很有趣,是一个很好的工具。

But if you want to just match whole lines that don't start with abcd, an easy way to do this is to match lines that do start with abcd, then take every line that doesn't match.

但是如果你想匹配不以abcd开头的整行,一个简单的方法是匹配以abcd开头的行,然后取不匹配的每一行。

Example (python):

例子(python):

In [1]: lines = [
   ...: "abcd 1",
   ...: "abcd 2",
   ...: "pqrs 3",
   ...: "pqrs 4" ]

In [2]: import re

In [4]: for line in lines:
   ...:     if re.match(r"^abcd.+$", line):
   ...:         pass # do nothing
   ...:     else:
   ...:         print (line)
   ...: 

pqrs 3
pqrs 4

Additionally, if the abcd you're looking for is a literal string (i.e. literally abcd, and not some other regular expression) then a string operation will be faster and easier to understand:

此外,如果您要查找的abcd是一个字面字符串(即字面上的abcd,而不是其他一些正则表达式),那么字符串操作将会更快、更容易理解:

In [5]: for line in lines:
   ...:     if not(line.startswith('abcd')):
   ...:         print(line)
   ...: 

pqrs 3
pqrs 4

#1


2  

[^\s]*(?<!abcd).efgh.ijkl

http://rubular.com/r/h11pUhuYSD

http://rubular.com/r/h11pUhuYSD

Should work for your purposes. It even matches if the target is in a longer string.

应该为你的目的工作。如果目标在较长的字符串中,它甚至可以匹配。

#2


1  

You want to use a lookahead for this, not a lookbehind.

你需要使用一个前视,而不是后视。

^(?!abcd\.)[a-z]+(?:\.[a-z]+)+$

The main regex is ^[a-z]+(?:\.[a-z]+)+$, which matches a string consisting of two or more clumps of letters separated by dots. The lookahead right after the start anchor ensures that the first clump is not abcd.

主regex ^[a - z]+(?:\[a - z]+)+ $匹配字符串组成的两个或两个以上的字母来表示。开始锚点之后的前视确保第一个块不是abcd。

Be aware that, if it's really Ruby you're doing this in, ^ and $ are line anchors. That means the regex would pluck the second line out of the string:

请注意,如果是真的Ruby你这样做,^和$行锚。这意味着regex将从字符串中提取第二行:

foo
pqrs.efgh.ijkl
bar

...which might not be what you want. To make sure you only match whole strings in Ruby, you should use the string anchors, \A and \z:

…这可能不是你想要的。为了确保在Ruby中只匹配整个字符串,您应该使用字符串锚,\A和\z:

\A(?!abcd\.)[a-z]+(?:\.[a-z]+)+\z

As for your attempt to use conditionals, Ruby doesn't seem to support them. But it doesn't matter, that wouldn't have worked anyway.

至于您尝试使用条件语句,Ruby似乎不支持它们。但没关系,反正那也没用。

#3


0  

Try this:

试试这个:

(?m)^(?!abcd).+$

(?)^(? ! abcd)+ $

Explanation:

解释:

<!--
(?m)^(?!abcd).+$

Options: ^ and $ match at line breaks

Match the remainder of the regex with the options: ^ and $ match at line breaks (m) «(?m)»
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!abcd)»
   Match the characters “abcd” literally «abcd»
Match any single character that is not a line break character «.+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
-->

#4


0  

Try this one:

试试这个:

[^"(a.+b)|(b.+c)|(c.+d)|"].* 

http://rubular.com/r/51OShSXwUz

http://rubular.com/r/51OShSXwUz

#5


0  

Negative lookbehinds are fun and they're a good tool to use.

负面的相貌很有趣,是一个很好的工具。

But if you want to just match whole lines that don't start with abcd, an easy way to do this is to match lines that do start with abcd, then take every line that doesn't match.

但是如果你想匹配不以abcd开头的整行,一个简单的方法是匹配以abcd开头的行,然后取不匹配的每一行。

Example (python):

例子(python):

In [1]: lines = [
   ...: "abcd 1",
   ...: "abcd 2",
   ...: "pqrs 3",
   ...: "pqrs 4" ]

In [2]: import re

In [4]: for line in lines:
   ...:     if re.match(r"^abcd.+$", line):
   ...:         pass # do nothing
   ...:     else:
   ...:         print (line)
   ...: 

pqrs 3
pqrs 4

Additionally, if the abcd you're looking for is a literal string (i.e. literally abcd, and not some other regular expression) then a string operation will be faster and easier to understand:

此外,如果您要查找的abcd是一个字面字符串(即字面上的abcd,而不是其他一些正则表达式),那么字符串操作将会更快、更容易理解:

In [5]: for line in lines:
   ...:     if not(line.startswith('abcd')):
   ...:         print(line)
   ...: 

pqrs 3
pqrs 4