Java 如何使用 String.split 提取方括号之间的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19751892/
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
How to extract text between square brackets with String.split
提问by Dev-otchka
I have text which contains tags expressed in this format: [text other text]
.
我有包含以这种格式表示的标签的文本:[text other text]
.
I'd like to split the string using square brackets as separators, but this:
我想使用方括号作为分隔符分割字符串,但是:
String.split("\[|\]");
This doesn't give expected results.
这不会给出预期的结果。
How can I do this?
我怎样才能做到这一点?
采纳答案by Andrey Chaschev
I'm not sure if one can do this with split()
. With pattern finding and [^\\]]+
("all symbols until the closing bracket") in your pattern this is quite straight-forward:
我不确定是否可以使用split()
. 在您的模式中使用模式查找和[^\\]]+
(“所有符号直到右括号”),这非常简单:
public static void main(String[] args) {
String line = "xx [text other text], [jili u babusi dva veselikh gusya], " +
"[a granny there was having two gay gooses] zz";
Matcher matcher = Pattern.compile("\[([^\]]+)").matcher(line);
List<String> tags = new ArrayList<>();
int pos = -1;
while (matcher.find(pos+1)){
pos = matcher.start();
tags.add(matcher.group(1));
}
System.out.println(tags);
}
回答by Dark Knight
You can use Pattern.quote("YOUR PATTERN GOES HERE"), as mentioned below,
您可以使用 Pattern.quote("YOUR PATTERN GOES HERE"),如下所述,
str.split(Pattern.quote("["));
回答by Rohit Jathot
you can use the split method and put the answer in an array
您可以使用 split 方法并将答案放入数组中
String [] d = s.split("(\\[)|(\\])");
String [] d = s.split("(\\[)|(\\])");
回答by Yury
I know this was asked 4 years ago, but for anyone with the same/similar question that lands here (as I did), there is something even simpler than using regex:
我知道这是 4 年前提出的问题,但是对于任何遇到相同/类似问题的人(就像我所做的那样),还有比使用正则表达式更简单的方法:
String result = StringUtils.substringBetween(str, "[", "]");
In your example, result
would be returned as "text other text", which you can further split using StringUtils.split() if needed. I would recommend the StringUtils library for various kinds of (relatively simple) string manipulation; it handles things like null input automatically, which can be convenient.
在您的示例中,result
将作为“文本其他文本”返回,如果需要,您可以使用 StringUtils.split() 进一步拆分。我会推荐 StringUtils 库用于各种(相对简单的)字符串操作;它自动处理诸如空输入之类的事情,这很方便。
Documentation for substringBetween(): https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#substringBetween-java.lang.String-java.lang.String-java.lang.String-
substringBetween() 的文档:https: //commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#substringBetween-java.lang.String-java.lang.String- java.lang.String-
There are two other versions of this function, depending on whether the opening and closing delimiters are the same, and whether the delimiter(s) occur(s) in the target string multiple times.
此函数还有另外两个版本,具体取决于开始和结束分隔符是否相同,以及分隔符是否在目标字符串中多次出现。