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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/719117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:29:41  来源:igfitidea点击:

Regular expression to match strings enclosed in square brackets or double quotes

javaregex

提问by Click Upvote

I need 2 simple reg exps that will:

我需要 2 个简单的 reg exp,它们将:

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

回答by Peter Boughton

\[\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

下一次,您应该能够通过阅读正则表达式.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.

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

回答by Chas. Owens

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.

  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?

  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

  1. 可以[]嵌套(例如[foo [bar]])?如果是这样,那么传统的正则表达式帮不了你。Perl 的扩展正则表达式可以,但最好编写一个解析器。

  2. 可以[, ], 或"出现"foo said \"bar\""在字符串中转义(例如)吗?如果是这样,请参阅如何将双引号字符串与转义的双引号字符匹配?

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

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

根据评论,您似乎想要匹配诸如 "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";

回答by Chas. Owens

Are they two separate expressions?

它们是两个不同的表达式吗?

[[A-Za-z]+]

[[A-Za-z]+]

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

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

If they are in a single expression:

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

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

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

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

请记住,在 .net 中,您需要将双引号 "" 转义为 ""