gpt4 book ai didi

Java 模式打印捕获组

转载 作者:行者123 更新时间:2023-11-30 11:26:29 25 4
gpt4 key购买 nike

((\d{1,2})/(\d{1,2})/(\d{2,4}))

有没有办法用 Pattern 对象检索所有捕获组的列表。我调试了对象,它只说有多少个组 (5)。

我需要检索以下捕获组的列表。

输出示例:

0 ((\d{1,2})/(\d{1,2})/(\d{2,4}))
1 (\d{2})/(\d{2})/(\d{4})
2 \d{2}
3 \d{2}
4 \d{4}

更新:

我不一定要问是否存在正则表达式,但那将是最有利的。到目前为止,我已经创建了一个只匹配最内部组的基本解析器(我不检查大多数越界条件)。我想知道是否有办法保留对已访问括号的引用。我可能必须实现树结构?

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class App {

public final char S = '(';
public final char E = ')';
public final char X = '\\';

String errorMessage = "Malformed expression: ";

/**
* Actual Output:
* Groups: [(//), (\d{1,2}), (\d{1,2}), (\d{2,4})]
* Expected Output:
* Groups: [\\b((\\d{1,2})/(\\d{1,2})/(\\d{2,4}))\\b, ((\\d{1,2})/(\\d{1,2})/(\\d{2,4})), (\d{1,2}), (\d{1,2}), (\d{2,4})]
*/

public App() {
String expression = "\\b((\\d{1,2})/(\\d{1,2})/(\\d{2,4}))\\b";
String output = "";

if (isValidExpression(expression)) {
List<String> groups = findGroups(expression);
output = "Groups: " + groups;
} else {
output = errorMessage;
}

System.out.println(output);
}

public List<String> findGroups(String expression) {
List<String> groups = new ArrayList<>();
int[] pos;
int start;
int end;
String sub;
boolean done = false;

while (expression.length() > 0 && !done) {
pos = scanString(expression);
start = pos[0];
end = pos[1];

if (start == -1 || end == -1) {
done = true;
continue;
}

sub = expression.substring(start, end);
expression = splice(expression, start, end);
groups.add(0, sub);
}

return groups;
}

public int[] scanString(String str) {
int[] range = new int[] { -1, -1 };
int min = 0;
int max = str.length() - 1;
int start = min;
int end = max;
char curr;

while (start <= max) {
curr = str.charAt(start);
if (curr == S) {
range[0] = start;
}
start++;
}

end = range[0];

while (end > -1 && end <= max) {
curr = str.charAt(end);
if (curr == E) {
range[1] = end + 1;
break;
}

end++;
}

return range;
}

public String splice(String str, int start, int end) {
if (str == null || str.length() < 1)
return "";

if (start < 0 || end > str.length()) {
System.err.println("Positions out of bounds.");
return str;
}

if (start >= end) {
System.err.println("Start must not exceed end.");
return str;
}

String first = str.substring(0, start);
String last = str.substring(end, str.length());

return first + last;
}

public boolean isValidExpression(String expression) {
try {
Pattern.compile(expression);
} catch (PatternSyntaxException e) {
errorMessage += e.getMessage();
return false;
}

return true;
}

public static void main(String[] args) {
new App();
}
}

最佳答案

这是我的解决方案...我只是提供了正则表达式的正则表达式,@SotiriosDelimanolis 已将其注释掉。

public static void printGroups() {
String sp = "((\\(\\\\d\\{1,2\\}\\))\\/(\\(\\\\d\\{1,2\\}\\))\\/(\\(\\\\d\\{2,4\\}\\)))";
Pattern p = Pattern.compile(sp);
Matcher m = p.matcher("(\\d{1,2})/(\\d{1,2})/(\\d{2,4})");
if (m.matches())
for (int i = 0; i <= m.groupCount(); i++)
System.out.println(m.group(i));
}

请注意,您不能删除 if 语句,因为要使用 group 方法,您应该首先调用 matches 方法(我不知道!)。参见 this link作为引用。

希望这就是您要的...

关于Java 模式打印捕获组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19790210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com