gpt4 book ai didi

java - 正则表达式模式元数据

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:55 25 4
gpt4 key购买 nike

我想检查正则表达式以识别它包含的匹配组。以下是我想要的 API 类型的示例:

String pattern = "^My name is \"([^\"]*)\" and I am (\d*) years old$"
Pattern p = Pattern.compile(pattern)

Group g1 = p.getGroups(0); // Representing the name group
g1.getStartPosition(); // should yeild position in regex string, e.g. 14
g1.getEndPosition(); // 21

Group g2 = p.getGroups(1); // Representing the age group
g2.getStartPosition(); // 34
g2.getEndPosition(); // 39

java 标准 java.util.regex.Pattern 没有提供此功能,但我想知道是否有任何现有的开源库允许我以这种方式检查正则表达式?

我宁愿避免自己动手,尝试使用 java.lang.String API 来分离正则表达式字符串,因为这会特别麻烦。

最佳答案

这不是专业的 API,但我鼓励您尝试一下这个类(class)。我把它作为练习,它有几个类似于 Matcher 的方法,例如:group(int group)start(int group)end(int group)groupCount()。它相当容易使用。

import java.util.ArrayList;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Metamatcher {
private String pattern;
private TreeMap<Integer,Integer> groupsIndices;

Metamatcher(String pattern){
this.pattern = pattern;
groupsIndices = getGroups();
}
/**
* @param group ordinal number of group
* @return starting index of a fragment of pattern, which contain group capturing
*/
public int start(int group){
ArrayList<Integer> indices = new ArrayList<Integer>(groupsIndices.keySet());
indices.add(0,0);
return indices.get(group);
}

/**
* @param group ordinal number of group
* @return ending index of a fragment of pattern, which contain group capturing
*/
public int end(int group){
ArrayList<Integer> indices = new ArrayList<Integer>(groupsIndices.values());
indices.add(0,pattern.length());
return indices.get(group);
}

/**
* @param group ordinal number of group
* @return String object containing fragment of regular expression which capture given group
*/
public String group(int group){
return pattern.substring(start(group), end(group));
}

/**
* @return number of capturing groups within given regular expression
*/
public int groupCount(){
return groupsIndices.size();
}

public String toString(){
StringBuilder result = new StringBuilder();
result.append("Groups count: ")
.append(groupCount())
.append("\n");
for(int i = 0; i <= groupCount(); i++){
result.append("group(")
.append(i).append(") ")
.append(start(i))
.append("-")
.append(end(i))
.append("\t")
.append(group(i))
.append("\n");
}
return result.toString();
}

/**It extracts fragments of regular expression enclosed by parentheses, checks if these are capturing type,
* and put start and end indices into Map object
* @return Map contains fragments of regular expression which capture groups
*/
private TreeMap<Integer,Integer> getGroups(){
String copy = pattern;
Pattern pattern = Pattern.compile("\\([^\\(\\)]+\\)");
Matcher matcher = pattern.matcher(copy);
TreeMap<Integer,Integer> temp = new TreeMap<Integer,Integer>();

while(matcher.find()){
if(isCapturingGroup(matcher.group(0))){
temp.put(matcher.start(), matcher.end());
}
copy = copy.substring(0,matcher.start()) + replaceWithSpaces(matcher.group(0)) + copy.substring(matcher.end());
matcher = pattern.matcher(copy);
}

return temp;
}

/**
* @param fragment of regular expression, enclosed by brackets
* @return true if given String consist regular expression which capture groups
*/
private boolean isCapturingGroup(String fragment){
return fragment.matches("((?<!\\\\)\\((?!\\?<?[:=!])[^\\(\\)]+\\))");
}

/**
* Provide a filler String composed of spaces, to replace part enclosed by brackets
* @param part String containing starting and ending with brackets,
* @return String composed of spaces (' '), with length of part object,
*/
private String replaceWithSpaces(String part){
String filler = "";
for(int i = 0; i < part.length(); i++){
filler += " ";
}
return filler;
}
}

我使用各种输入对其进行了测试,并将输出与 regex101 等工具进行了比较,它对我有用。

关于java - 正则表达式模式元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699232/

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