正则表达式匹配括在方括号或双引号中的字符串

时间:2022-09-15 16:00:36

I need 2 simple reg exps that will:

我需要2个简单的reg exps:

  1. Match if a string is contained within square brackets ([] e.g [word])
  2. 如果字符串包含在方括号内,则匹配([]例如[word])

  3. Match if string is contained within double quotes ("" e.g "word")
  4. 如果字符串包含在双引号内,则匹配(“”例如“word”)

3 个解决方案

#1


\[\w+\]

"\w+"


Explanation:

The \[ and \] escape the special bracket characters to match their literals.

\ [和\]转义特殊括号字符以匹配其文字。

The \w means "any word character", usually considered same as alphanumeric or underscore.

\ w表示“任何单词字符”,通常被认为与字母数字或下划线相同。

The + means one or more of the preceding item.

+表示前一项中的一个或多个。

The " are literal characters.

“是文字字符。


NOTE: If you want to ensure the whole string matches (not just part of it), prefix with ^ and suffix with $.

注意:如果要确保整个字符串匹配(不仅仅是其中的一部分),则前缀为^,后缀为$。


And next time, you should be able to answer this yourself, by reading regular-expressions.info

下次,你应该能够通过阅读regular-expressions.info自己回答这个问题

Update:

Ok, so based on your comment, what you appear to be wanting to know is if the first character is [ and the last ] or if the first and last are both " ?
If so, these will match those:

好吧,所以根据你的评论,你似乎想知道的是第一个字符是[和最后一个]还是第一个和最后一个都是“?如果是,那么这些将匹配:

^\[.*\]$    (or ^\\[.*\\]$ in a Java String)

"^.*$"

However, unless you need to do some special checking with the centre characters, simply doing:

但是,除非您需要对中心字符进行一些特殊检查,否则只需执行以下操作:

if ( MyString.startsWith("[") && MyString.endsWith("]") )

and

if ( MyString.startsWith("\"") && MyString.endsWith("\"") )

Which I suspect would be faster than a regex.

我怀疑它会比正则表达式更快。

#2


Important issues that may make this hard/impossible in a regex:

在正则表达式中可能使这很难/不可能的重要问题:

  1. Can [] be nested (e.g. [foo [bar]])? If so, then a traditional regex cannot help you. Perl's extended regexes can, but it is probably better to write a parser.

    可以[]嵌套(例如[foo [bar]])吗?如果是这样,那么传统的正则表达式无法帮助你。 Perl的扩展正则表达式可以,但编写解析器可能更好。

  2. Can [, ], or " appear escaped (e.g. "foo said \"bar\"") in the string? If so, see How can I match double-quoted strings with escaped double-quote characters?

    可以[,]或“在字符串中出现转义(例如”foo say \“bar \”“)吗?如果是,请参阅如何将双引号字符串与转义双引号字符匹配?

  3. Is it possible for there to be more than one instance of these in the string you are matching? If so, you probably want to use the non-greedy quantifier modifier (i.e. ?) to get the smallest string that matches: /(".*?"|\[.*?\])/g

    是否有可能在匹配的字符串中有多个这样的实例?如果是这样,您可能希望使用非贪婪量词修饰符(即?)来获得匹配的最小字符串:/(“。*?”|| [。[*。])/ g

Based on comments, you seem to want to match things like "this is a "long" word"

根据评论,你似乎想要匹配“这是一个”长“字”之类的东西

#!/usr/bin/perl

use strict;
use warnings;

my $s = 'The non-string "this is a crazy "string"" is bad (has own delimiter)';

print $s =~ /^.*?(".*").*?$/, "\n";

#3


Are they two separate expressions?

它们是两个独立的表达吗?

[[A-Za-z]+]

\"[A-Za-z]+\"

If they are in a single expression:

如果它们在一个表达式中:

[[\"]+[a-zA-Z]+[]\"]+

Remember that in .net you'll need to escape the double quotes " by ""

请记住,在.net中你需要通过双引号“by”“

#1


\[\w+\]

"\w+"


Explanation:

The \[ and \] escape the special bracket characters to match their literals.

\ [和\]转义特殊括号字符以匹配其文字。

The \w means "any word character", usually considered same as alphanumeric or underscore.

\ w表示“任何单词字符”,通常被认为与字母数字或下划线相同。

The + means one or more of the preceding item.

+表示前一项中的一个或多个。

The " are literal characters.

“是文字字符。


NOTE: If you want to ensure the whole string matches (not just part of it), prefix with ^ and suffix with $.

注意:如果要确保整个字符串匹配(不仅仅是其中的一部分),则前缀为^,后缀为$。


And next time, you should be able to answer this yourself, by reading regular-expressions.info

下次,你应该能够通过阅读regular-expressions.info自己回答这个问题

Update:

Ok, so based on your comment, what you appear to be wanting to know is if the first character is [ and the last ] or if the first and last are both " ?
If so, these will match those:

好吧,所以根据你的评论,你似乎想知道的是第一个字符是[和最后一个]还是第一个和最后一个都是“?如果是,那么这些将匹配:

^\[.*\]$    (or ^\\[.*\\]$ in a Java String)

"^.*$"

However, unless you need to do some special checking with the centre characters, simply doing:

但是,除非您需要对中心字符进行一些特殊检查,否则只需执行以下操作:

if ( MyString.startsWith("[") && MyString.endsWith("]") )

and

if ( MyString.startsWith("\"") && MyString.endsWith("\"") )

Which I suspect would be faster than a regex.

我怀疑它会比正则表达式更快。

#2


Important issues that may make this hard/impossible in a regex:

在正则表达式中可能使这很难/不可能的重要问题:

  1. Can [] be nested (e.g. [foo [bar]])? If so, then a traditional regex cannot help you. Perl's extended regexes can, but it is probably better to write a parser.

    可以[]嵌套(例如[foo [bar]])吗?如果是这样,那么传统的正则表达式无法帮助你。 Perl的扩展正则表达式可以,但编写解析器可能更好。

  2. Can [, ], or " appear escaped (e.g. "foo said \"bar\"") in the string? If so, see How can I match double-quoted strings with escaped double-quote characters?

    可以[,]或“在字符串中出现转义(例如”foo say \“bar \”“)吗?如果是,请参阅如何将双引号字符串与转义双引号字符匹配?

  3. Is it possible for there to be more than one instance of these in the string you are matching? If so, you probably want to use the non-greedy quantifier modifier (i.e. ?) to get the smallest string that matches: /(".*?"|\[.*?\])/g

    是否有可能在匹配的字符串中有多个这样的实例?如果是这样,您可能希望使用非贪婪量词修饰符(即?)来获得匹配的最小字符串:/(“。*?”|| [。[*。])/ g

Based on comments, you seem to want to match things like "this is a "long" word"

根据评论,你似乎想要匹配“这是一个”长“字”之类的东西

#!/usr/bin/perl

use strict;
use warnings;

my $s = 'The non-string "this is a crazy "string"" is bad (has own delimiter)';

print $s =~ /^.*?(".*").*?$/, "\n";

#3


Are they two separate expressions?

它们是两个独立的表达吗?

[[A-Za-z]+]

\"[A-Za-z]+\"

If they are in a single expression:

如果它们在一个表达式中:

[[\"]+[a-zA-Z]+[]\"]+

Remember that in .net you'll need to escape the double quotes " by ""

请记住,在.net中你需要通过双引号“by”“