正则表达式逐行:如何匹配三引号但不匹配双引号

发布于 2024-08-06 02:29:25 字数 96 浏览 2 评论 0原文

我需要检查一个由许多单词/字母/等组成的字符串是否只包含一组三重双引号(即“””),但也可以包含单双引号(“)和双双引号( ""),使用正则表达式。到目前为止还没有取得太大成功。

I need to check to see if a string of many words / letters / etc, contains only 1 set of triple double-quotes (i.e. """), but can also contain single double-quotes (") and double double-quotes (""), using a regex. Haven't had much success thus far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

·深蓝 2024-08-13 02:29:26

尝试使用出现次数运算符来精确匹配三个双引号。

  • \"{3}
  • ["]{3}
  • [\"]{3}

我已经使用 http://www.regextester.com/" rel="nofollow noreferrer">http:// /www.regextester.com/,似乎工作正常,

但如何用您选择的语言正确编译正则表达式可能会有所不同!

Try using the number of occurrences operator to match exactly three double-quotes.

  • \"{3}
  • ["]{3}
  • [\"]{3}

I've quickly checked using http://www.regextester.com/, seems to work fine.

How you correctly compile the regex in your language of choice may vary, though!

千紇 2024-08-13 02:29:26

取决于您的语言,但您应该只需要匹配三个双引号(例如,/\"{3}/),然后计算匹配数以查看是否正好有一个。

Depends on your language, but you should only need to match for three double quotes (e.g., /\"{3}/) and then count the matches to see if there is exactly one.

書生途 2024-08-13 02:29:26

可能有很多方法可以做到这一点,但一种简单的方法是仅查找多次出现的三引号,然后反转正则表达式。这是 Perl 的一个例子:

use strict;
use warnings;

my $match = 'hello """ hello "" hello';
my $no_match = 'hello """ hello """ hello';
my $regex = '[\"]{3}.*?[\"]{3}';

if ($match !~ /$regex/) {
    print "Matched as it should!\n";
}
if ($no_match !~ /$regex/) {
    print "You shouldn't see this!\n";
}

输出:

Matched as it should!

基本上,你告诉它找到你不想要的东西,然后颠倒事实。希望这是有道理的。如果您需要帮助,可以帮助您将示例转换为另一种语言。

There are probably plenty of ways to do this, but a simple one is to merely look for multiple occurrences of triple quotes then invert the regular expression. Here's an example from Perl:

use strict;
use warnings;

my $match = 'hello """ hello "" hello';
my $no_match = 'hello """ hello """ hello';
my $regex = '[\"]{3}.*?[\"]{3}';

if ($match !~ /$regex/) {
    print "Matched as it should!\n";
}
if ($no_match !~ /$regex/) {
    print "You shouldn't see this!\n";
}

Which outputs:

Matched as it should!

Basically, you are telling it to find the thing you DON'T want, then inverting the truth. Hope that makes sense. Can help you convert the example to another language if you need help.

ペ泪落弦音 2024-08-13 02:29:26

这对您来说可能是一个好的开始。

^(\"([^\"\n\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*\"|'([^'\n\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*'|\"\"\"((?!\"\"\")[^\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*\"\"\")$

请访问 regex101.com 查看实际情况。

This may be a good start for you.

^(\"([^\"\n\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*\"|'([^'\n\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*'|\"\"\"((?!\"\"\")[^\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*\"\"\")$

See it in action at regex101.com.

瞄了个咪的 2024-08-13 02:29:25

带有负向先行的正则表达式可以做到这一点:

(?!.*"{3}.*"{3}).*"{3}.*

我用这些 java 代码行尝试了它:

String good = "hello \"\"\" hello \"\" hello ";
String bad = "hello \"\"\" hello \"\"\" hello ";
String regex = "(?!.*\"{3}.*\"{3}).*\"{3}.*";
System.out.println( good.matches( regex ) );
System.out.println( bad.matches( regex ) );

...输出:

true
false

A regex with negative lookahead can do it:

(?!.*"{3}.*"{3}).*"{3}.*

I tried it with these lines of java code:

String good = "hello \"\"\" hello \"\" hello ";
String bad = "hello \"\"\" hello \"\"\" hello ";
String regex = "(?!.*\"{3}.*\"{3}).*\"{3}.*";
System.out.println( good.matches( regex ) );
System.out.println( bad.matches( regex ) );

...with output:

true
false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文