Java 在一行中捕捉相似的单词

Java 在一行中捕捉相似的单词,java,regex,pattern-matching,Java,Regex,Pattern Matching,我有下面的字符串 What is (Jim)'s gift (limit)? <=> Personname <=> Amount::Spent 预期产出: String is Personname <=> Amount::Spent 2 2 start position is 9 end position is 12 Entity Type Personname start position is 22 end position is 27 Enti

我有下面的字符串

What is (Jim)'s gift (limit)? <=> Personname <=> Amount::Spent
预期产出:

String is  Personname <=> Amount::Spent
2   2
start position is 9 end position is 12 Entity Type Personname 
start position is 22 end position is 27 Entity Type Amount::Spent
字符串是Personname金额::花费
2   2
起始位置为9结束位置为12实体类型Personname
起始位置为22结束位置为27实体类型金额::已用
电流输出

String is  Personname <=> Amount::Spent
2   2
start position is 9 end position is 12 Entity Type Personname 
start position is 22 end position is 27 Entity Type Personname 
start position is 9 end position is 12 Entity Type Amount::Spent
start position is 22 end position is 27 Entity Type Amount::Spent
2   2
start position is 9 end position is 12 Entity Type Personname 
start position is 22 end position is 27 Entity Type Personname 
start position is 9 end position is 12 Entity Type Amount::Spent
start position is 22 end position is 27 Entity Type Amount::Spent
字符串是Personname金额::花费
2   2
起始位置为9结束位置为12实体类型Personname
起始位置为22结束位置为27实体类型Personname
起始位置为9结束位置为12实体类型金额::已用
起始位置为22结束位置为27实体类型金额::已用
2   2
起始位置为9结束位置为12实体类型Personname
起始位置为22结束位置为27实体类型Personname
起始位置为9结束位置为12实体类型金额::已用
起始位置为22结束位置为27实体类型金额::已用
请让我知道我哪里出了问题,我怎样才能解决这个问题


谢谢

您需要删除2个循环

  • “for(字符串拆分字符串:示例_实体)”
  • “while(实体计数<实体数量)”

  • String line=“什么是(Rakesh)的礼物(限额)?Personname金额::花费”;
    如果(第行包含(“”)){
    String[]示例_split=line.split(“,2);
    System.out.println(“字符串为”+示例_split[1]);
    如果(例如分割[0]。长度()>1){
    字符串[]示例\实体=示例\拆分[1]。拆分(“”);
    int openParamCount=line.length()-line.replace(“(”,”).length();
    int closeParamCount=line.length()-line.replace(“(”,”).length();
    System.out.println(openParamCount+“\t”+closeParamCount);
    如果(!(openParamCount==closeParamCount))
    System.out.println(“不匹配”+行);
    如果(!(openParamCount==示例_entity.length))
    System.out.println(
    “所提供的实体和副词中标记的单词与“+行”不匹配);
    整数单位计数=0;
    int起始位置;
    int-endPosition=0;
    列表匹配列表=新的ArrayList();
    Pattern regex=Pattern.compile(“\\(.*?\)”);
    Matcher regexMatcher=regex.Matcher(行);
    while(regexMatcher.find()){
    startPosition=regexMatcher.start()+1;
    endPosition=regexMatcher.start()-1;
    添加(regexMatcher.group(1));
    System.out.println(“起始位置为”+startPosition+”结束位置为“+endPosition
    +“实体类型”+示例实体[实体数量];
    }
    实体计数++;
    }
    }
    
    但是,您的代码表明括号将始终是闭合的,例如,它不允许为内部循环留出空间

    什么是(吉姆)和(凯尔)的礼物(限额)

    不会返回正确的结果。但这只是一个问题,如果您希望以该形式输入

    String is  Personname <=> Amount::Spent
    2   2
    start position is 9 end position is 12 Entity Type Personname 
    start position is 22 end position is 27 Entity Type Personname 
    start position is 9 end position is 12 Entity Type Amount::Spent
    start position is 22 end position is 27 Entity Type Amount::Spent
    2   2
    start position is 9 end position is 12 Entity Type Personname 
    start position is 22 end position is 27 Entity Type Personname 
    start position is 9 end position is 12 Entity Type Amount::Spent
    start position is 22 end position is 27 Entity Type Amount::Spent
    
        String line = "What is (Rakesh)'s gift (limit)? <=> Personname <=> Amount::Spent";
        if (line.contains("<=>")) {
            String[] example_split = line.split("<=>", 2);
            System.out.println("String is " + example_split[1]);
            if (example_split[0].length() > 1) {
                String[] example_entity = example_split[1].split("<=>");
    
                int openParamCount = line.length() - line.replace("(", "").length();
                int closeParamCount = line.length() - line.replace("(", "").length();
                System.out.println(openParamCount + "\t" + closeParamCount);
                if (!(openParamCount == closeParamCount))
                    System.out.println("Paranthesis don't match for " + line);
                if (!(openParamCount == example_entity.length))
                    System.out.println(
                            "The entities provided and the words marked in paranthesis don't match for " + line);
    
                int entities_count = 0;
                int startPosition;
                int endPosition = 0;
                List<String> matchList = new ArrayList<>();
                Pattern regex = Pattern.compile("\\((.*?)\\)");
                Matcher regexMatcher = regex.matcher(line);
                while (regexMatcher.find()) {
                    startPosition = regexMatcher.start() + 1;
                    endPosition = regexMatcher.start() - 1;
    
                    matchList.add(regexMatcher.group(1));
                    System.out.println("start position is " + startPosition + " end position is " + endPosition
                            + " Entity Type" + example_entity[entities_count]);
                }
                entities_count++;
            }
        }