dig*_*nie 130 java regex string
如果我有这样的字符串:
FOO[BAR]
Run Code Online (Sandbox Code Playgroud)
我需要一种通用的方法来从字符串中获取"BAR"字符串,这样无论方括号之间的字符串是什么,它都能够获得字符串.
例如
FOO[DOG] = DOG
FOO[CAT] = CAT
Run Code Online (Sandbox Code Playgroud)
Bry*_*yle 241
你应该能够使用非贪婪的量词,特别是*?. 你可能想要以下内容:
Pattern MY_PATTERN = Pattern.compile("\\[(.*?)\\]");
Run Code Online (Sandbox Code Playgroud)
这将为您提供一个与您的字符串匹配的模式,并将文本放在第一组的方括号内.有关更多信息,请查看Pattern API文档.
要提取字符串,您可以使用以下内容:
Matcher m = MY_PATTERN.matcher("FOO[BAR]");
while (m.find()) {
String s = m.group(1);
// s now contains "BAR"
}
Run Code Online (Sandbox Code Playgroud)
zac*_*zap 31
非正则表达方式:
String input = "FOO[BAR]", extracted;
extracted = input.substring(input.indexOf("["),input.indexOf("]"));
Run Code Online (Sandbox Code Playgroud)
或者,为了更好的性能/内存使用(感谢Hosam):
String input = "FOO[BAR]", extracted;
extracted = input.substring(input.indexOf('['),input.lastIndexOf(']'));
Run Code Online (Sandbox Code Playgroud)
Dja*_*kka 26
这是一个有效的例子:
RegexpExample.java
package org.regexp.replace;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExample
{
public static void main(String[] args)
{
String string = "var1[value1], var2[value2], var3[value3]";
Pattern pattern = Pattern.compile("(\\[)(.*?)(\\])");
Matcher matcher = pattern.matcher(string);
List<String> listMatches = new ArrayList<String>();
while(matcher.find())
{
listMatches.add(matcher.group(2));
}
for(String s : listMatches)
{
System.out.println(s);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它显示:
value1
value2
value3
Run Code Online (Sandbox Code Playgroud)
如果您只需要获得介于两者之间的任何内容[],您可以\[([^\]]*)\]像这样使用:
Pattern regex = Pattern.compile("\\[([^\\]]*)\\]");
Matcher m = regex.matcher(str);
if (m.find()) {
result = m.group();
}
Run Code Online (Sandbox Code Playgroud)
如果您需要它的形式,identifier + [ + content + ]那么只有当标识符是字母数字时才限制提取内容:
[a-zA-Z][a-z-A-Z0-9_]*\s*\[([^\]]*)\]
Run Code Online (Sandbox Code Playgroud)
这将验证诸如Foo [Bar]或类似的事情myDevice_123["input"].
主要问题
主要问题是当你想要提取这样的内容时:
FOO[BAR[CAT[123]]+DOG[FOO]]
Run Code Online (Sandbox Code Playgroud)
正则表达式不会工作,将返回BAR[CAT[123和FOO.
如果我们将正则表达式更改为\[(.*)\]那么我们就可以了,但是如果您尝试从更复杂的内容中提取内容,例如:
FOO[BAR[CAT[123]]+DOG[FOO]] = myOtherFoo[BAR[5]]
Run Code Online (Sandbox Code Playgroud)
没有一个正则表达式会起作用.
在所有情况下提取适当内容的最准确的正则表达式会更加复杂,因为它需要平衡[]对并为您提供内容.
更简单的解决方案
如果你的问题变得复杂并且[]任意的内容,你可以[]使用普通的旧代码比正则表达式平衡对并提取字符串:
int i;
int brackets = 0;
string c;
result = "";
for (i = input.indexOf("["); i < str.length; i++) {
c = str.substring(i, i + 1);
if (c == '[') {
brackets++;
} else if (c == ']') {
brackets--;
if (brackets <= 0)
break;
}
result = result + c;
}
Run Code Online (Sandbox Code Playgroud)
这是比实际代码更多的伪代码,我不是Java编码器,所以我不知道语法是否正确,但它应该很容易改进.
重要的是这个代码应该工作,并允许您提取它的内容[],无论它多么复杂.
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public static String get_match(String s, String p) {
// returns first match of p in s for first group in regular expression
Matcher m = Pattern.compile(p).matcher(s);
return m.find() ? m.group(1) : "";
}
get_match("FOO[BAR]", "\\[(.*?)\\]") // returns "BAR"
public static List<String> get_matches(String s, String p) {
// returns all matches of p in s for first group in regular expression
List<String> matches = new ArrayList<String>();
Matcher m = Pattern.compile(p).matcher(s);
while(m.find()) {
matches.add(m.group(1));
}
return matches;
}
get_matches("FOO[BAR] FOO[CAT]", "\\[(.*?)\\]")) // returns [BAR, CAT]
Run Code Online (Sandbox Code Playgroud)